harish
harish

Reputation: 1765

Parsing jsonarray in android

i need to parse the json array contacts from the below json

{

    },
    //Truncated for clarity
   ]
}

Upvotes: 0

Views: 553

Answers (3)

i think this isnt not a valid Json. normal Json must have a "Label" : "value". Label and value must be seperated by colon :

"w","www.firmdale.com"

Should be like this

"w":"www.firmdale.com"

http://code.google.com/p/jsonvalidator/

Edited: to be more clear and specific as per Aroth

object
    {}
    { members } 
members
    pair
    pair , members
pair
    string : value
array
    []
    [ elements ]
elements
    value
    value , elements
value
    string
    number
    object
    array
    true
    false
    null 

Upvotes: 1

aroth
aroth

Reputation: 54856

Parsing is easy. First get json-simple and add it to your project (or you can use the built-in JSON library, if you like how everything in that library can throw a checked JSONException).

Next, your entire response-text is a valid JSON object. So do:

JSONObject response = (JSONObject)JSONValue.parse(responseText);

Your object has an array of results under the "result" key. You can get that like:

JSONArray results = (JSONArray)response.get("result");

Your array contains a set of objects that you want to iterate over to get the contact info for each one. For example:

for (Object obj : results) {
    JSONObject entry = (JSONObject)obj;
    JSONArray contacts = (JSONArray)entry.get("contact");
    for (Object contact : contacts) {
        System.out.println(contact);
    }
}

Note that whomever put that JSON together decided to use an array type for holding each individual contact entry. An object type would have made a lot more sense, but as-is, contact.get(0) will give you the type-code for the contact entry (t, w, etc.), and contact.get(1) will give you the actual contact number/address/whatever.

Upvotes: 4

Sandip Jadhav
Sandip Jadhav

Reputation: 7155

 private JSONObject jObject;
 jObject = new JSONObject(your response as string);
 //this will give you json object for HotelInformationResponse
 JSONArray menuitemArray = popupObject.getJSONArray("result"); 
 // loop through this array to get all values 

for extra information Check this link

Upvotes: 0

Related Questions