0

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

{

    },
    //Truncated for clarity
   ]
}
7
  • That's valid JSON according to jsonlint, what's the problem? Commented Apr 11, 2012 at 6:52
  • What is the issue you are having parsing this? Commented Apr 11, 2012 at 6:52
  • i need to parse the jsonarray contacts Commented Apr 11, 2012 at 6:53
  • i get all the datas when parsing but in contacts array i need to get the "t" and "w" separately Commented Apr 11, 2012 at 6:54
  • i used the getString() but when "t"is not "w" is getting as "t" data Commented Apr 11, 2012 at 6:55

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 

4 Comments

It's valid JSON, but logically they should have used a JSON object instead of a JSON array for storing the individual contact entries.
You should format your output as valid JSON even if it's single string. stackoverflow.com/questions/5950297/… see link's comment specially.
ya read it... can i der be any solution or is it needed to change in webservice
0
 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.