i need to parse the json array contacts from the below json
{
},
//Truncated for clarity
]
}
-
That's valid JSON according to jsonlint, what's the problem?aroth– aroth2012年04月11日 06:52:26 +00:00Commented Apr 11, 2012 at 6:52
-
What is the issue you are having parsing this?Quintin Robinson– Quintin Robinson2012年04月11日 06:52:27 +00:00Commented Apr 11, 2012 at 6:52
-
i need to parse the jsonarray contactsharish– harish2012年04月11日 06:53:47 +00:00Commented 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" separatelyharish– harish2012年04月11日 06:54:57 +00:00Commented Apr 11, 2012 at 6:54
-
i used the getString() but when "t"is not "w" is getting as "t" dataharish– harish2012年04月11日 06:55:43 +00:00Commented Apr 11, 2012 at 6:55
3 Answers 3
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.
Comments
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
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