简体   繁体   中英

How do I parse a string of json if I do not know its object model before hand?

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

This may be what you are looking for

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

Cheers

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, ie

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM