Take the 2-minute tour ×

Possible Duplicate:
JSON Array iteration in Android/Java

I am fetching JSON string from server and I have already got JSON string by code. But I didn't understand how to parse it.

Below is my JSON string

    "university": {
        "name": "oxford",
        "url": "http://www.youtube.com"
    "1": {
        "id": "2",
        "title": "Baseball",
        "datetime": "2011-11-11 10:41:46"
    "2": {
        "id": "1",
        "title": "Two basketball team players earn all state honors",
        "datetime": "2011-11-11 10:40:57"

Please provide any guidance or code snippet.

share|improve this question

marked as duplicate by casperOne Sep 11 '12 at 11:26


2 Answers 2

up vote 34 down vote accepted

Use JSON classes for parsing e.g

JSONObject mainObject = new JSONObject(Your_Sring_data);
JSONObject uniObject = mainObject.getJSONObject("university");
String  uniName = uniObject.getJSONObject("name");
String uniURL = uniObject.getJSONObject("url");

JSONObject oneObject = mainObject.getJSONObject("1");
String id = oneObject.getJSONObject("id");
share|improve this answer
can we get how many objects in json string –  helloDroid Nov 11 '11 at 8:11
Yes you can simply use "mainObject.length();" –  Arslan Nov 11 '11 at 8:12
thanks.. it's working fine –  helloDroid Nov 11 '11 at 8:22

Below is the link which guide in parsing JSON string in android.


Also according to your json string code snippet must be something like this:-

JSONObject mainObject = new JSONObject(yourstring);

JSONObject universityObject = mainObject.getJsonObject("university");
JSONString name = universityObject.getJsonString("name");  
JSONString url = universityObject.getJsonString("url");

Same for other object.

share|improve this answer
