如果我事先不知道它的对象模型,如何解析 json 字符串?

发布于 2025-01-03 15:45:13 字数 153 浏览 4 评论 0原文

我想使用开放街道地图 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热风软妹 2025-01-10 15:45:13

这可能就是您要查找的内容

http://code.google.com/p/google- gson/

干杯

This may be what you are looking for

http://code.google.com/p/google-gson/

Cheers

狼亦尘 2025-01-10 15:45:13

首先,您需要知道 JSON 文件是否包含数组或对象。如果第一个非空白字符是 [,则它是一个数组,如果它是 {,则它是一个对象。当第一个字符是 { 时创建 JSONArray 或反之亦然将引发运行时异常。

其次,一旦你有了 JSONObject,你就会想要从中获取数据。所以你必须知道键的名称才能获取值,即

myStreet = myJsonOjbect.getString("street name")

如果你不打算从中获取数据,那么拥有 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.

myStreet = myJsonOjbect.getString("street name")

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文