0

How to fetch this data using json.Is it an object or an array? I am confused. Below is my data and what I have implemented. I am unable to get any values. Please help me to fetch the values.

    JSONObject jobject = new JSONObject(response);
    JSONArray jsonArray = jobject.getJSONArray("variety");
    for (int i =0; i<=jsonArray.length();i++){
        jobject=  jsonArray.getJSONObject(i);
        txt_today_671.setText(jobject.getString("variety.coc671"));
      }

     {
    "status": 200,
    "variety": {
        "coc671": {
            "today": 0,
            "todate": 0
        },
        "co92005": {
            "today": 0,
            "todate": 0
        },
     },
        "others": {
            "today": 0,
            "todate": 0
        }
    },
    "distance": {
        "0to20": {
            "today": 0,
            "todate": 0
        },
        "20to40": {
            "today": 0,
            "todate": 0
        },
    "above100": {
            "today": 0,
            "todate": 0
        }
    }
}
Pranav P
  • 1,865
  • 18
  • 37
Aparna Mutnalkar
  • 147
  • 1
  • 14
  • 1
    The json Object starts from {} and Array starts from [] , so look again now and tell what is it – A.s.ALI Dec 10 '18 at 05:17
  • @Shararti KAKI - All are objects – Aparna Mutnalkar Dec 10 '18 at 05:18
  • yes , now you can get what you want – A.s.ALI Dec 10 '18 at 05:19
  • @Shararti KAKI - Help me fetch the values.. – Aparna Mutnalkar Dec 10 '18 at 05:19
  • Instead of `JSONArray jsonArray = jobject.getJSONArray("variety");` you should fetch it like `JSONObject object = jobject.getJSONObject("variety");` variety is `JSONObject` not `JSONArray` everything which is wrapped in {} is Object and everything which is wrapped in [] is an array. And instead of looping you can iterate over an object. look at this answer for that https://stackoverflow.com/a/10593838/4985413 – Prashant Dec 10 '18 at 05:21
  • @AparnaMutnalkar see this example yout will get more idea about json parsing https://www.androidhive.info/2012/01/android-json-parsing-tutorial/ – Vishal Thakkar Dec 10 '18 at 05:44

2 Answers2

3

Here, variety and distance is not jsonArray, it's a jsonObject.

To iterate through jsonObject follow below mechanism-

JSONObject varietyObject = jobject.getJSONObject("variety");
Iterator<JSONObject> keysVariety = varietyObject.keys();

while(keysVariety.hasNext()) {
String keyVariety = keysVariety.next();
    JSONObject objectVariety = varietyObject.getJSONObject(keyVariety);
    // here you will get inner String/int values
    int today = objectVariety.getInt("today");
    int todate = objectVariety.getInt("todate");
}

Do same for distance object-

JSONObject distanceObject = jobject.getJSONObject("distance");
Iterator<JSONObject> keysDistance = distanceObject.keys();

while(keysDistance.hasNext()) {
String keyDistance = keysDistance.next();
    JSONObject objectDistance = distanceObject.getJSONObject(keyDistance);
    // here you will get inner String/int values
    int today = objectDistance.getInt("today");
    int todate = objectDistance.getInt("todate");
}

Always remember JSONArray starts with [ and JSONObject start with {

Exigente05
  • 2,128
  • 3
  • 19
  • 37
0

to just illustrate you a little , here in the demo there could be typo mistakes,

JSONObject jobject = new JSONObject(response); // here you are getting all your object in one object
JSONObject varityObject= jobject.getJSONObject("variety");
//NOW USE varityObject to get coc671 and  co92005
//Similarly 
JSONObject distanceObject= jobject.getJSONObject("distance");
// now use distanceObject to get its items 
A.s.ALI
  • 1,869
  • 3
  • 17
  • 45