简体   繁体   中英

Retrieving result from JSON to ArrayList

I have the following JSON- 在此处输入图片说明

  1. journeys
    • {obj} 0
      • [ ] legs
        • {obj} 0
          • {obj} instruction
          • "string" detailed : string value goes here0
        • {obj} 1
          • {obj} instruction
          • "string" detailed : string value goes here1
    • {obj} 1
      • [ ] legs
        • {obj} 0
          • {obj} instruction
          • "string" detailed : string value goes here0
        • {obj} 1
          • {obj} instruction
          • "string" detailed : string value goes here1
    • {obj} 2
      • [ ] legs
        • {obj} 0
          • {obj} instruction
          • "string" detailed : string value goes here0
        • {obj} 1
          • {obj} instruction
          • "string" detailed : string value goes here1

My plan is to iterate through values "journeys" and store them in an ArrayList . Similarly, store "legs" in another ArrayList and finally get retrieve the value for the object instruction . As you can see parent journeys has 3 child {obj} 0 , {obj} 1 , {obj} 2 . I can get the journeys stored using --

ArrayList<JSONObject> journey = new ArrayList<>();
ArrayList<JSONArray> leg = new ArrayList<>();
ArrayList<JSONObject> legDetails = new ArrayList<>();

try {
            JSONArray jArray = object.getJSONArray("journeys");
            if (jArray != null) {
                for (int i=0;i<jArray.length();i++){
                    journey.add(jArray.getJSONObject(i));
                }
                if (!journey.isEmpty()){
                    for (int j=0;j<journey.size();j++){
                         leg.add(journey.get(j).getJSONArray("legs"));
                }
            }
            if (!leg.isEmpty()){
                for (int i = 0; i<leg.size();i++){
                     legDetails.add(leg.get(i).getJSONObject(i));
                    }
            }
          }
        } 
        catch (JSONException e) {
            e.printStackTrace();
        }

Now each journey returns 2 legs and the number changes time to time. I was expecting 6 JSONObject at the end of last for loop but I only get 2 and a out of range exception.

Things to remember: Journey has Routes (Obj 0,1,2). Routes has legs (Obj 0,1). Legs consists of instruction with detail string.

Is this right approach to retrieve the value? and what am I doing wrong?

problem is here

        if (!journey.isEmpty()){

          for (int j=0;j<journey.size();j++){
                   leg.add(journey.get(j).getJSONArray("legs"));

                }

        }

journery's have 3 objects.

for j=0 you add journey[0] array to leg
for j=1 you add journey[1] array to leg
for j=2 you add journey[2] array to leg

so legs will have 3 arrays.

if (!leg.isEmpty()){
   for (int i = 0; i<leg.size();i++){
      legDetails.add(leg.get(i).getJSONObject(i));
   }
}

here leg.size()=3;

for i=0 it will add leg[0]'s [0]th element
for i=1 it will add leg[1]'s [1]th element
for i=2 it will add leg[2]'s [2]th element (this is out of range)

update ::

if (!leg.isEmpty()){
   for (int i = 0; i<leg.size();i++){
      for(int j=0;j<leg.get(i).size();j++)
         legDetails.add(leg.get(i).getJSONObject(j));
   }
}

This should help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM