AnonTCS
AnonTCS

Reputation: 3

Parse nested JSON with varying number of entries

I'm working on a bus transit app and one of the network calls I need to make gets the trip info on how to get from point A to point B. The bus transit API has a method that provides this info. The method returns up to 3 viable itineraries, and each itinerary has a variable amount of legs. There are also two types of legs, walking legs and riding legs.

I was wondering on how I should go about parsing this data and then transferring it to the UI. What's throwing me off is that the nested entries also have nested arrays of variable length (that can be of either two types). To first retrieve the data I think using an ArrayList of ArrayList could work. Should I then create two models that correspond to the two type of legs, add the data to those, and then dynamically add those to the listView I'm going to use to display the results? (I would also have to dynamically add the itineraries).

Oh and another thing. If the bus you need to get on finishes a route and starts a new one (while you need to stay on it the whole time) It will return multiple services in a single leg and I have no idea how to efficiently check for that and convey that to the user.

This is what I have so far (only have code for parsing right now).

 final String JSON_ITINERARIES = "itenararies";
            final String JSON_STOP_NAME = "stop_name";
            final String JSON_STOP_LAT = "stop_lat";
            final String JSON_STOP_LONG = "stop_long";
            final String JSON_STOP_ID = "stop_id";


            try {
                JSONArray itinerariesArray;
                JSONObject stopsJson = new JSONObject(JsonStr);

                


            itinerariesArray = stopsJson.getJSONArray(JSON_ITINERARIES);

            ArrayList itinerariesArrayList = new ArrayList<ArrayList<String>>(itinerariesArray.length());

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


                JSONObject singleTrip = itinerariesArray.getJSONObject(i);
                JSONArray singleTripLegsJSON = singleTrip.getJSONArray("legs");
                ArrayList legsArrayList = new ArrayList<String>(singleTripLegsJSON.length());

                for (int j=0; j<singleTripLegsJSON.length(); j++) {
                    JSONObject SingleTripObjectSingleLeg = singleTripLegsJSON.getJSONObject(j);
                    String TYPE = SingleTripObjectSingleLeg.getString("type")
                                        //?????   


                }

Upvotes: 0

Views: 101

Answers (1)

Stanley Ko
Stanley Ko

Reputation: 3497

I recommand you to use GSON. GSON is very very very useful tool for JSON.


First, use http://www.jsonschema2pojo.org/ to make class from json.

Copy & paste your json string to this site,
select "source type : JSON",
select "Annotation style: GSON",
press "Preview" OR "JAR" to get JAVA classes.

Second, use GSON to parse your json string. Visit Here to get more examples.

example:
private Gson gson = new Gson();

String sampleData = //
        "[" //
                + "{\"info\": {\"place\": \"place1\"},"
                + "\"events\": {\"info\": \"555f1fc297f229004dd6e8aa\",\"time\": \"5\",\"image\": \"555f1fc2d197270b6c732d3b\",\"event_name\": \"555f1fc224d1cb629a8ef603\"}},"
                + "{\"info\": {\"place\": \"place3\"},"
                + "\"events\": {\"info\": \"555f1fc283c7b150ede89c05\",\"time\": \"7\",\"image\": \"555f1fc2bf5fa8a3b320e0ca\",\"event_name\": \"555f1fc20d40f1b478610505\"}},"
                + "{\"info\": {\"place\": \"place2\"},"
                + "\"events\": {\"info\": \"555f1fc29163e85ae42e7518\",\"time\": \"6\",\"image\": \"555f1fc21506a186c2d34a92\",\"event_name\": \"555f1fc272a06e68b8c3f4b7\"}}" + "  ]";

    java.lang.reflect.Type listType = new TypeToken<List<Event>>() {
    }.getType();
    ArrayList<Event> eventList = gson.fromJson(sampleData, listType);

Upvotes: 2

Related Questions