简体   繁体   中英

Parsing JSON file in Android Application

How would I parse the following string in Android?

{
    "C1": {
        "name": "first name",
        "address": "first address",
        "lat": 36.072111,
        "lng": 34.732112
    },
    "C2": {
        "name": "second name",
        "address": "second address",
        "lat": 32.02132,
        "lng": 34.000002
    },
    "C3": {
        "name": "third name",
        "address": "third address",
        "lat": 37.05435,
        "lng": 34.75703
    }
}

I can't understand. Is it an objects inside of an object structure? How would this be parsed? How do I find how many objects I have?

Well, got it. the solution is to first get the names of the inner-objects:

JONObject json = new JSONObject(jsonString);
JSONArray namesArray = json.names();

which will give you an JSONArray of the existing objects inside. Then run on it's objects to get each one of them:

for (int i = 0 ; i < namesArray.length() ; i ++)
{
    currentObject = json.getJSONObject(namesArray.get(i).toString());
    Log.d("TAG", "currentObject : "+currentObject.toString());          
    addCurrentObjectShopToObjectsListUsingGson(currentObject,objectsList);
}

You can use JSONObject to extract the contents of the structure.

An example can be shown below:

You can retrieve a JSONArray from your string with

JSONObject json = new JSONObject(jsonString);
JSONArray myArray = json.getJSONArray(ARRAY_NAME_HERE);

After doing so, you can extract the name of a person with

JSONObject person = myArray.getJSONObject(0); // retrieve the first person
String name = person.getString("name"); // get the person's name

Reference:

http://developer.android.com/reference/org/json/JSONObject.html

The string you've shown contains an outer object with 3 inner objects. Suppose you want to get C1.name. You would do this:

JSONObject root = new JSONObject(yourString);
JSONObject c1 = root.getJSONObject("C1");
String name = c1.getString("name");

However, I should point out one other thing, which is that the original string you are using is odd because it suggests that what you really want is an array. The code to parse would be different, of course, and involve JSONArray, but I think a better representation would look like this:

  [
   {"name":"first name","address":"...","lat":"...","lng":"..."},
   {"name":"second name"...},
   {"name":"third name"...}
  ]

So in this case, the outermost container is a JSONArray, not an object.

You need a "model" object that looks like this: (provided the hash is static).

public class TheCs extends BaseModel {
    public OneC c1;
    public OneC c2;
    public OneC c3;
}

public class OneC extends BaseModel {
    public String name;
    public String address;
    public float lat, lng;
}

public class BaseModel implements Serializable {
    private static final long serialVersionUID = 1L;//use a random number here
}

Now when parsing with Gson, pass TheCs.class as the type.

If the Hash is not static you could (and Gson will do the right thing as far as I can remember), do something like:

public class TheCs extends BaseModel {
    public List<OneC> someHash;
}

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