-1

I have a JSON string like this which is received from a server:

[{"AdsId":"7","AdsName":"PIC_01.JPG","AdsImage":"pic_01.jpg","AdsImageUrl":"http://www.google.com","TargetUrl":"http://www.yahoo.com","IsAllPageSupport":false,"IsArabic":false,"IsActive":true},{"AdsId":"8","AdsName":"PIC_02.JPG","AdsImage":"pic_02.jpg","AdsImageUrl":"http://www.fb.com","TargetUrl":"http://www.twitter.com","IsAllPageSupport":true,"IsArabic":false,"IsActive":true},{"AdsId":"9","AdsName":"PIC_03.JPG","AdsImage":"pic_03.jpg","AdsImageUrl":"http://www.google.com","TargetUrl":"http://www.demo.com","IsAllPageSupport":false,"IsArabic":false,"IsActive":true},{"AdsId":"10","AdsName":"PIC_04.JPG","AdsImage":"pic_04.jpg","AdsImageUrl":"http://www.sample.com","TargetUrl":"http://www.example.com","IsAllPageSupport":true,"IsArabic":false,"IsActive":true}]

This is a valid JSON format. But I don't know how to parse this string and retrieve the values inside this JSON string.

I'm using this code for parsing the JSON array. But this code is used for retrieving values from a JSON array, and I don't know how to get values from the above received JSON string.

JSONObject jsonResponse;

try {           
    jsonResponse = new JSONObject(status);

    JSONArray jsonMainNode = jsonResponse.optJSONArray("Android");

    int lengthJsonArr = jsonMainNode.length();

    Log.d("Json Array Length of status",String.valueOf(lengthJsonArr));

    for(int j1=0; j1 < lengthJsonArr; j1++) {

        JSONObject jsonChildNode = jsonMainNode.getJSONObject(j1);                      
        String addstatus=jsonChildNode.optString("slno").toString();
    }
 } catch(Exception ex) {
     ex.printStackTrace();
 }
halfer
  • 19,824
  • 17
  • 99
  • 186
njnjnj
  • 978
  • 4
  • 23
  • 58
  • Have a look at this [post](http://stackoverflow.com/questions/9605913/how-to-parse-json-in-android). And if you don't want to parse every single JSON-object by yourself, you can use a JSON mapper library such as [Jackson](http://jackson.codehaus.org) or [GSON](https://code.google.com/p/google-gson/) – blackfizz Sep 30 '14 at 07:48
  • In ur Json, slno is not there and ur trying to fetch the value and if it is not there. it may throw null and ur again doing toString. so ur app will crash. check before getting anything with jsonChildNode.has("slno") then u do ur logic in the braces – Harsha Vardhan Sep 30 '14 at 07:50
  • its a typo error brother – njnjnj Sep 30 '14 at 07:51
  • I take this from another code just to show you... – njnjnj Sep 30 '14 at 07:52

3 Answers3

2

Try this code

String s = "[{'AdsId':'7','AdsName':'PIC_01.JPG','AdsImage':'pic_01.jpg','AdsImageUrl':'http://www.google.com','TargetUrl':'http://www.yahoo.com','IsAllPageSupport':false,'IsArabic':false,'IsActive':true},{'AdsId':'8','AdsName':'PIC_02.JPG','AdsImage':'pic_02.jpg','AdsImageUrl':'http://www.fb.com','TargetUrl':'http://www.twitter.com','IsAllPageSupport':true,'IsArabic':false,'IsActive':true},{'AdsId':'9','AdsName':'PIC_03.JPG','AdsImage':'pic_03.jpg','AdsImageUrl':'http://www.google.com','TargetUrl':'http://www.demo.com','IsAllPageSupport':false,'IsArabic':false,'IsActive':true},{'AdsId':'10','AdsName':'PIC_04.JPG','AdsImage':'pic_04.jpg','AdsImageUrl':'http://www.sample.com','TargetUrl':'http://www.example.com','IsAllPageSupport':true,'IsArabic':false,'IsActive':true}]";
        JSONArray array;
        try {
            array = new JSONArray(s);

            for (int i = 0; i < array.length(); i++) {
                Log.v(i + "AdsId", array.getJSONObject(i)
                        .getString("AdsId"));
                Log.v(i + "AdsName",
                        array.getJSONObject(i).getString("AdsName"));
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Result

0AdsId: 7
0AdsName: PIC_01.JPG
1AdsId: 8
1AdsName: PIC_02.JPG
2AdsId: 9
2AdsName): PIC_03.JPG
3AdsId: 10
3AdsName: PIC_04.JPG
mujeeb.omr
  • 499
  • 2
  • 12
0

You should create JSONArray object.

JSONArray ja = new JSONArray(satus); //check the array is not empty and execute ja.optJSONObject(0).optString("AdsId");

kemenov
  • 409
  • 4
  • 13
0

Easy way? Use a higher-level JSON object mapper like Jackson (https://github.com/FasterXML/jackson):

ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
ArrayList<Map<String, Object>> list = mapper.readValue(status, ArrayList.class);
// You can now iterate over or randomly access the list
String adsName = list.get(0).get("AdsName");

If you want to do it without adding a custom library your code is a good start, but you have many issues in the code itself. Your use of Logging is wrong - the first parameter is the log tag (which is typically the class name), the second parameter the string to log. Try this:

try {

  final JSONArray jsonArray = new JSONArray(inputString);

  final int lengthJsonArr = jsonArray.length();

  Log.d("JSONParse", "Json Array Length of status " + lengthJsonArr);

  for(int i = 0; i < lengthJsonArr; i++) {
    // This is an object from the array.
    JSONObject jsonChildNode = jsonArray.getJSONObject(i);
    // Retrieve any value from the object like this
    String adName = jsonChildNode.optString("AdsName").toString();
  }
} catch(Exception ex)
{
  Log.e("JSONParse", "Cannot read JSON string", ex);
}

Familiarize yourself with using Androids excellent Javadocs at developer.android.com, for example http://developer.android.com/reference/org/json/JSONArray.html

Kevin Read
  • 2,173
  • 16
  • 23