简体   繁体   中英

Accessing an object from JSON in Android

I have a question that I am a little bit confused about. I am quite new to JSON and getting JSON values in the android API. I am trying to access an array within the response I get. the JSON code I am getting is something like this:

Response:
{
"event": {  
    "participants": []
},
"status": "success"
}

How would I access the participants array and store their values. This is what I am trying at the moment... but I dont appear to be getting what I want.

try{
                //get the JSON values from the URL.
                JSONObject json = jParser.getJSONFromUrl("http://somesite.com/api/find?"+"somevar="+someJavaStringVar);

                json_event = json.getJSONObject("event");

                JSONArray json_array_participants = json_event.getJSONArray("participants");  

} catch(JSONException e) {

}

The thing I am mostly confused about is... what is the arrays type equivalent to. Any advice or reasoning as to the correct way to get ahold of that variables value would be great... thanks guys.. :).

I would strongly recommend to use gson instead as your preferred parser since it will do all the job of serializing and deserializing for you except creating the domain objects. This tutorial should get you going: http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

This will depend on what the server is supposed to return. It could be an array of anything and if this is a public service, there should be a specification to go off of.

If you are in charge of the server portion as well, and you have a backing object, Google's GSON library is extremely easy to use. It will also keep type information straight.

Think JSON is really just a key-value pairing. The JSONArray type is just an array full of objects (like Object[] ) - it has no idea what the objects it contains are or what they're to be used for. Its up to you to assign meaning to the JSON stream based on what you know of the source. From what I see of your code, most of it looks fine, though I don't know what your jParser.getJSONFromURL() is doing. Typically, you would build the JSON from the response string like so:

String jsonString = getJSONFromUrl("http://somesite.com/api/find?"+"somevar="+someJavaStringVar);

JSONObject json = new JSONObject(jsonString)

JSONObject json_event = json.getJSONObject("event");

JSONArray json_array_participants = json_event.getJSONArray("participants");

You can iterate through the array like any other array to get subobjects or whatever:

for(int i=0; i < json_array_participants.getLength(); i++) {
  JSONObject participant = json_array_participants.getJSONObject(i);
  // Do stuff
}

As a side note - I WOULDN'T use GSON until you understand the underlying protocol, at least a little - because you never know when you might want to parse your JSON from a different language for some reason.

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