如果我事先不知道它的对象模型,如何解析 json 字符串?
我想使用开放街道地图 (OSM)。 OSM 通过使用键值对来尽可能保持其数据格式的灵活性。我正在开发一个 Android 应用程序,我将向它发送一个 OSM 数据的 JSON 字符串。如果我事先不知道 JSON 是什么样子该怎么办?最好的图书馆是什么?
感谢您的帮助, 克里斯
I want to work with Open Street Map (OSM). OSM keeps its data formats as flexible as possible by using key value pairs. I am developing an application for Android and I am going to send it a JSON string of OSM data. What should I do if I do not know what the JSON will look like in advance? What would be the best library?
Thanks for your help,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能就是您要查找的内容
http://code.google.com/p/google- gson/
干杯
This may be what you are looking for
http://code.google.com/p/google-gson/
Cheers
首先,您需要知道 JSON 文件是否包含数组或对象。如果第一个非空白字符是 [,则它是一个数组,如果它是 {,则它是一个对象。当第一个字符是 { 时创建 JSONArray 或反之亦然将引发运行时异常。
其次,一旦你有了 JSONObject,你就会想要从中获取数据。所以你必须知道键的名称才能获取值,即
如果你不打算从中获取数据,那么拥有 json 文件有什么意义呢?当然,您可以在 Lint 中打开 JSON 以查看其结构。
希望这有帮助!
First of all, you need to know if the JSON file contains an array or an object. If the first nonwhite space character is a [, it's an array, if it's a {, it's an object. Creating JSONArray when the first char is a { or vice versa will throw a Runtime Exception.
Second off all, once you have your JSONObject, you're going to want to get data from it. So you have to know the name of the keys to get the values, i.e.
If you're not going to get data from it, what's the point of having the json file? Surely you can open the JSON in a Lint to see what the structure is.
hope this helps!