Here is my JSON object and i want to parse it in android project
{
"second": {
"versionInfo": "0.20.3-dev",
"compileDate": "Mon Mar 12 17:39:23 IST 2012",
"compileUser": "suraj",
"trackname": "tracker_localhost:localhost/127.0.0.1:48418"
},
"href": {
"versionInfo": "null",
"compileDate": "null",
"compileUser": "null",
"trackname": "null"
},
"first": {
"key": "['trackname','versionInfo','compileDate','compileUser']"
}
}
How to compile? i want to first extract attributes of 'first' and using the attributes and then to extract parameters of 'second' using attributes of 'first'.
-
Have you considered using a JSON parser: stackoverflow.com/questions/338586/a-better-java-json-libraryassylias– assylias2012年03月12日 12:43:51 +00:00Commented Mar 12, 2012 at 12:43
-
Also, you can make use of GSON to parse this JSON String into Java object types and manipulate it. java.sg/parsing-a-json-string-into-an-object-with-gson-easilyHJW– HJW2012年03月12日 12:44:14 +00:00Commented Mar 12, 2012 at 12:44
-
the best way would be to use Gson and let it do automatically for you.waqaslam– waqaslam2012年03月12日 12:44:20 +00:00Commented Mar 12, 2012 at 12:44
-
stackoverflow.com/questions/9441932/…Yaqub Ahmad– Yaqub Ahmad2012年03月12日 13:43:43 +00:00Commented Mar 12, 2012 at 13:43
1 Answer 1
Basically it is done like that:
JSONObject jobj = new JSONObject(theString);
JSONObject first = jobj.getJSONObject("first");
JSONObject second = jobj.getJSONObject("second");
If you want more, take a look at the documentation of JSON classes for android.
Edit
Regarding the extraction of the array (in first->key):
String jStr = first.getString("key");
JSONArray jArr = new JSONArray(jStr);
answered Mar 12, 2012 at 12:43
MByD
138k30 gold badges269 silver badges278 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
user1264099
thanks....but how to get values from key 'key' in 'first' object....as there is only one key and value is array of attributes?
MByD
one at a time :). The value of "key" is NOT an array but a String (that contains a serialized JSON array). I will edit in a few minutes, but if you read the documentation it'll be easier for you in the future.
user1264099
thank you very much....actually i am new to json and android thats why i dont know the exact concept.........anyway i will keep in mind your suggestion..and again thanks for help :)
default