0

I'm trying to extract the "stops" array from the JSON Array, but it's contained within another array called "results". How do I get the "stops" and put them into a JSON array?

The variables have been declared further up the code (not shown). There is also catches (not shown).

The code is giving me the error is:

2018-12-19 14:31:38.892 6650-6787/ie.[college].student.[studentid].dublinbuses E/StopIdResultsActivity: An error occurred! Error: No value for stops.

The part of code I need looked at is:

protected Void doInBackground(Void... arg0) {
            ...
            ...
                try {

                    origin = "";
                    destination = "";
                    route_result = "";

                    JSONObject jsonObj = new JSONObject(routeid_jsonStr);

                    ...

                    }
                    else {
                    JSONArray results = jsonObj.getJSONArray("results");

                     //looping through All Contacts
                    for (int i = 0; i < results.length(); i++) {

                        JSONObject r = results.getJSONObject(i);
                        JSONArray stops = jsonObj.getJSONArray("stops");
                        for (int j = 0; j < stops.length(); j++) {

                            JSONObject s = stops.getJSONObject(j);

                            stopid = r.getString("stopid");
                            shortname = r.getString("shortname");
                            shortnamelocalized = r.getString("shortnamelocalized");

                            HashMap<String, String> stop = new HashMap<>();

                            // adding each child node to HashMap key => value
                            stop.put("stopid", stopid);
                            stop.put("shortname", shortname);
                            stop.put("shortnamelocalized", shortnamelocalized );

                            resultList.add(stop);
                        }
                    }
                }

Examples: JSON OR XML

{
errorcode: "0",
errormessage: "",
numberofresults: 4,
route: "77a",
timestamp: "19/12/2018 13:03:06",
results: [
{
operator: "bac",
origin: "Citywest",
originlocalized: "Iarthar na Cathrach ",
destination: "Ringsend",
destinationlocalized: "",
lastupdated: "27/06/2016 09:02:52",
stops: [
    {
    stopid: "1358",
    displaystopid: "1358",
    shortname: "Dame Street",
    shortnamelocalized: "Sráid an Dáma",
    fullname: "Dame Street",
    fullnamelocalized: "",
    latitude: "53.34430611",
    longitude: "-6.262861111",
    operators: [
        {
        name: "bac",
        routes: [
                "77A"
                ]
        }
    ]
},

...
...
3
  • Please add a clear problem statement to your question. Note that a good explanation can sometimes be more useful than a bunch of code. Commented Dec 19, 2018 at 14:41
  • I updated my question. Commented Dec 19, 2018 at 14:45
  • have you tried turning JSONArray stops = jsonObj.getJSONArray("stops"); into JSONArray stops = r.getJSONArray("stops"); Commented Dec 19, 2018 at 14:48

2 Answers 2

1

Please have a look on these lines:

JSONObject r = results.getJSONObject(i);
JSONArray stops = jsonObj.getJSONArray("stops");

I think it should be instead:

JSONObject r = results.getJSONObject(i);
JSONArray stops = r.getJSONArray("stops");

And here you also mix up varialbles:

stopid = r.getString("stopid");
shortname = r.getString("shortname");
shortnamelocalized = r.getString("shortnamelocalized");

It should be:

stopid = s.getString("stopid");
shortname = s.getString("shortname");
shortnamelocalized = s.getString("shortnamelocalized");
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah that's what I'm thinking. Otherwise, you're just going back to your beginning object trying to access the inner array from the outermost layer.
Ok, that stops the error message, but it doesn't set the variables stop.put(...)
I think the problem with stop.put(...) is somewhere else. Please check the variables stopid, shortname and shortnamelocalized and see if they contain correct values.
Thank you for your help. :)
1

It looks like you use two different variables

JSONObject s = stops.getJSONObject(j);

stopid = r.getString("stopid");
shortname = r.getString("shortname");

You store the json stop data in sbut reads it from r, so it should be

JSONObject s = stops.getJSONObject(j);

stopid = s.getString("stopid");
shortname = s.getString("shortname");

2 Comments

Then what is r?
The "results" array. The "stops" array is contained within the "results".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.