1

I am trying to parse the following JSON data into a recycler view. In particular, I want to parse the address object to display the locationName, line1, city, state and zip, but I cant get the parsing method right.

   {
 "pollingLocations": [
  {
   "address": {
    "locationName": "LITTLE LEAGUE HEADQUARTERS",
    "line1": "6707 LITTLE LEAGUE DR",
    "city": "SAN BERNARDINO",
    "state": "CA",
    "zip": "92407"
   },
   "notes": "",
   "pollingHours": "07:00-20:00",
   "sources": [
    {
     "name": "Voting Information Project",
     "official": true
    }
   ]
  }
 ]
}

My issue is finding the address object. With the current code, I get a logcat error that says No value for locationName. I think this is because I first need to specify the address object to iterate through, but how do I do this? This is the current implementation that I have right now.

//fetch
private void getData() {
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Loading...");
    progressDialog.show();

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            progressDialog.dismiss();
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray array = jsonObject.getJSONArray("pollingLocations");
                for(int i = 0; i < array.length(); i++){
                    JSONObject addressObject = array.getJSONObject(i);
                    for (int x = 0; x < addressObject.length(); x++){
                        VotingLoc votingLoc = new VotingLoc(addressObject.getString("locationName"),
                                addressObject.getString("line1"),
                                addressObject.getString("city"),
                                addressObject.getString("state"),
                                addressObject.getString("zip"),
                                addressObject.getString("pollingHours"));
                        votingList.add(votingLoc);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Volley", error.toString());
            progressDialog.dismiss();
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

Locat Error: enter image description here

Is there an easier way of doing this? I believe the code for my rvadapter class and is correct and the error is in this method, but If you want to see any other code, let me know.

3
  • You are missing node address Commented Jan 7, 2019 at 19:15
  • Thats what I was thinking, but when I create a jsonObject for address the logcat error then says no value for address. Can you bhe a little more specific on how you would implement this? Commented Jan 7, 2019 at 19:17
  • Do i need a nested for loop in the scenario? How can I do it without one? Commented Jan 7, 2019 at 19:29

2 Answers 2

1

By getting first node in array you have obtained address node so no need to run loop there, address object is not an array you can get values directly. Try something like this:

        JSONArray array = object.getJSONArray("pollingLocations");
        for (int i = 0; i < array.length(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
           String locationName = jsonObject.getJSONObject("address")
                   .getString("locationName");
           Log.d("locationName", locationName);
            JSONArray sourcesArray = jsonObject.getJSONArray("sources");

          // run loop to get values from this array
          for (int j = 0; j < sourcesArray.length(); j++){
                JSONObject source = sourcesArray.getJSONObject(j);
                String name = source.getString("name");
            }
        }
Sign up to request clarification or add additional context in comments.

2 Comments

This was the first solution I had in mind, but whenever I try to create the JSONObject and pass "address" as a parameter I get a syntax error. I can only pass an integer as an index of the array and not a string.
I can pass index i from the for loop but it wont let me pass a string
1

Another way could be using a json parser like gson (https://github.com/google/gson) or jackson (https://github.com/FasterXML/jackson). This may be a bit overhead if your only goal is to show these few values, as you must create some java classes, you may only need for parsing.

Comments

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.