1

Possible Duplicate:
How to Parse a JSON Object In Android

i have JSON object like following .. How can i parse the Object?

{
  "0":
     {
       "productname":"Famous Amos Bite Size Chocolate Chip Cookies - 4 Pack",
       "imageurl":"http://ecx.images-amazon.com/images/I/513j-WyH1GL._SL160_.jpg",
       "producturl":"http://www.searchupc.com/rd.aspx?u=d%2bKvXQ%2fFIfa95xJ38QYLycSjbm5dt4dy3l4IYTYPM3agt4tefTNsMwzWkPWd9gCY%2fEnCdaGVMLsQD%2fO5ZUWbfJyqOuwIWqkLvouDyw5u7VWmda5dK2%2fRTmcAp3%2f1TImmZmtdaNauL74Lj8BkV0r15VeazeDf4Im4Nx%2f5TOuqBUnUXzeNkYrWvlLitV8FDFIkM77UIjZzYZqoQANt0PBNeqh94bzLqFRXpNYPyqc0fLDTHnA9TM2jsbaKVN23UA%2fH",
       "price":"5.95",
       "currency":"usd",
       "saleprice":"",
       "storename":"amazon.com"
     }
}
Community
  • 1
  • 1
Anand
  • 3,346
  • 9
  • 35
  • 54
  • 4
    https://www.google.com/search?q=android+parse+json – jolivier Aug 28 '12 at 10:21
  • See [this](http://www.androidhive.info/2012/01/android-json-parsing-tutorial/) And, see the images in there link. how they're taking json values using JSONObject. – Praveenkumar Aug 28 '12 at 10:21
  • see this question http://stackoverflow.com/questions/8381896/handle-server-response-in-android..upvote if it helps you – Shruti Aug 28 '12 at 10:21
  • Hi Visit the link [JSON PARSE ANDROID](http://www.androidhive.info/2012/01/android-json-parsing-tutorial/) A nice tutorial on json parsing in android. – Ashwin N Bhanushali Aug 28 '12 at 10:22
  • You might want to check out the GSON library: http://code.google.com/p/google-gson/ – AlexMok Aug 28 '12 at 10:23

5 Answers5

3

An example below demonstrates how to parse JSON. You will get a very clearcut explanation about json parsing here.

JSONObject myjson = new JSONObject("put you json string here");
JSONArray the_json_array = myjson.getJSONArray("0");
int size = the_json_array.length();
for (int i = 0; i < size; i++) {
JSONObject another_json_object = the_json_array.getJSONObject(i);           
    System.out.println((String) another_json_object.get("productname"));        
    System.out.println((String) another_json_object.get("imageurl"));       
    System.out.println((String) another_json_object.get("producturl"));
}
Vinay
  • 6,891
  • 4
  • 32
  • 50
2

Android provides the org.json package that can be used to parse your String to a JSONObject.

You can achieve this with this line of code:

JSONObject json = new JSONObject(myJsonString);
Ben Weiss
  • 17,182
  • 6
  • 67
  • 87
2

You've to use the JSONObject for this. Use of below code -

JSONParser .java

public JSONObject getJSONFromUrl(String url)  // url is your json url
{

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

it will return the JsonObject And, using this jsonbobject we can get the result as array.

// Creating JSON Parser instance
JSONParser jParser = new JSONParser();

// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);

try {
    // Getting Array of Contacts
    contacts = json.getJSONArray(TAG_CONTACTS);

    for(int i = 0; i < contacts.length(); i++) {

        String product_name = contacts.getString("productname");
        String image_url = contacts.getString("imageurl");
        .....
        .....
        String store_name = contacts.getString("storename");
    }
}catch(JSONException e) 
{
    e.printStackTrace();
}

For more just refer Android json tutorial

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
1

There is a working example YatayatService that uses json library and custom JsonParser.java using apache http library. The implementation method is getAllRoutes().

String jsonString = JsonParser.parseJSON(URL);
try {
    JSONObject parentObject = new JSONObject(jsonString);
 } catch (JSONException e) {
    e.printStackTrace();
 }
prayagupa
  • 30,204
  • 14
  • 155
  • 192
1

Try This code

 // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    System.out.println("json is "+json);
    System.out.println("Length"+json.length());

    for (int i = 0; i < json.length(); i++) {
        try {
            String atObj = Integer.toString(i);
            System.out.println(atObj);
            JSONObject jObj = json.getJSONObject(atObj);
            System.out.println(jObj.getString("productname"));
            System.out.println(jObj.getString("imageurl"));
            System.out.println(jObj.getString("producturl"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
Anand
  • 3,346
  • 9
  • 35
  • 54