2

Here I am getting following JSON from local asset folder. All the JSON object is different in their name. Line 1, 3, 5, 7 .... upto 1000. Herewith I have attached the json for your reference.

     {
    "status": {
        "rcode": 200,
        "message": "OK"
    },
    "data": {
        "0": {
            "OutletName": "Test 1 ",
            "Latitude": "16.123234",
            "Longitude": "79.546745"
        },
        "1": {
            "OutletName": "Test 2",
            "Latitude": "16.343234",
            "Longitude": "79.786745"
        },
        "2": {
            "OutletName": "Test 3",
            "Latitude": "19.1294",
            "Longitude": "72.836122"
        },
        "3": {
            "OutletName": "Test 4",
            "Latitude": "19.136383",
            "Longitude": "72.827997"
        },
        "6": {
            "OutletName": "Test 5",
            "Latitude": "19.136715",
            "Longitude": "72.829248"
        },
        "7": {
            "OutletName": "Test 6",
            "Latitude": "19.128483",
            "Longitude": "72.821199"
        },
        "8": {
            "OutletName": "Test 7",
            "Latitude": "19.128528",
            "Longitude": "72.819388"
        },
        "10": {
            "OutletName": "Test 8",
            "Latitude": "19.140333",
            "Longitude": "72.831095"
        },
        "11": {
            "OutletName": "Test 9",
            "Latitude": "19.14027",
            "Longitude": "72.826285"
        }
    }
}

Here the Object name is different for all. So I try like this

private void parseJson() {
        try {
            JSONObject obj = new JSONObject(loadJSONFromAsset());
            JSONObject jObj = obj.getJSONObject("data");
            System.out.println("jObj.length()==> " + jObj.length());
            for (int i = 0; i < jObj.length(); i++) {

                if (jObj.has(String.valueOf(i)) && !jObj.isNull(String.valueOf(i))) {
                    JSONObject jObj1 = jObj.getJSONObject(String.valueOf(i));
                    System.out.println("Index==> "+i);
                    System.out.println("OutletName==> "+jObj1.getString("OutletName"));
                  }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

But here I not getting all the data. Please help me to parse this kind of JSON.

Thanks in advance.

  • 1
    seems like your json is wrong. please post complete json or check it on http://jsonlint.com for any errors in json before parsing. – Jolson Da Costa Sep 13 '15 at 07:06
  • as point out by Jolson there is error in line "7":{ "OutletName":"Test 6", "Latitude":"19.128483", "Longitude":"72.821199", },......there , after end of longitude string...similar after other object as well rectify it first – Shadow Droid Sep 13 '15 at 07:10
  • As Jolson said your JSon object is wrong, you need to have a JSon Array for `data` Jsonkey – Abdul Rahman K Sep 13 '15 at 07:12
  • Sorry to all.. I have edited the json string. Sorry This is a testing/Sample json. Real json will come from server. That is also this same format. Please help me to parse this json. – Vijayadhas Chandrasekaran Sep 13 '15 at 07:22
  • Really, you should just grab whoever made that JSON structure and explain them friendly what an array is. – poss Sep 13 '15 at 07:24

3 Answers3

1

You need to use the keys method like this:

private void parseJson() {
    try {
        JSONObject obj = new JSONObject(loadJSONFromAsset());
        JSONObject jObj = obj.getJSONObject("data");
        Iterator iter = jObj.keys()
        while(iter.hasNext()) {
            String key = (String) iter.next();
            JSONObject jObj1 = jObj.getJSONObject(key);
            System.out.println("Index==> "+ key);
            System.out.println("OutletName==> "+jObj1.getString("OutletName"));
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Edit: fixed some type conversion

Derek Fung
  • 8,171
  • 1
  • 25
  • 28
0

The value of "data" in your JSON is not an array, hence you cannot iterate over it directly. You will need to first convert it into a HashMap and then iterate over the keyset. You can use Jackson library to convert JSONObject to HashMap. You can also refer to this answer for conversion to map.

Community
  • 1
  • 1
marrock
  • 301
  • 1
  • 2
  • 12
0

first of all you will have to do changes in your json response

 {
    "status": {
        "rcode": 200,
        "message": "OK"
    },
    "data": {                   //this has to be array in your json

        "0": {                            //  and these are object inside the arrays
            "OutletName": "Test 1 ",
            "Latitude": "16.123234",
            "Longitude": "79.546745"
        },
        "1": {                                    //  and these are object inside the arrays
            "OutletName": "Test 2",
            "Latitude": "16.343234",
            "Longitude": "79.786745"
        },
        "2": {
            "OutletName": "Test 3",
            "Latitude": "19.1294",
            "Longitude": "72.836122"
        },
        "3": {
            "OutletName": "Test 4",
            "Latitude": "19.136383",
            "Longitude": "72.827997"
        },
        "6": {
            "OutletName": "Test 5",
            "Latitude": "19.136715",
            "Longitude": "72.829248"
        },
        "7": {
            "OutletName": "Test 6",
            "Latitude": "19.128483",
            "Longitude": "72.821199"
        },
        "8": {
            "OutletName": "Test 7",
            "Latitude": "19.128528",
            "Longitude": "72.819388"
        },
        "10": {
            "OutletName": "Test 8",
            "Latitude": "19.140333",
            "Longitude": "72.831095"
        },
        "11": {
            "OutletName": "Test 9",
            "Latitude": "19.14027",
            "Longitude": "72.826285"
        }
    }
}

and dont over json object for its size to access its object it has to be JSONArray get the array data in JSONArray like this

JSONArray dataArray = response_obj.getJSONArray("data");

and then iterate like this

for (int i = 0; i < dataArray.length(); i++) 
{
   //write your code here to access all the object as for loop executes on by one
}
Jolson Da Costa
  • 1,095
  • 1
  • 12
  • 31