0

Am new to using json files and I have constructed a layered json object and i am trying to display the values in android studio Toast but some of the values which are key and I don't want them to be displayed yet but they are getting displayed in the process please guide on what am doing wrong

This is portion of the json file (single object in the json array)

 {
    "county": "Nyeri",
    "subcounty": ["Kieni East", [{
        "ward": ["funny", [{
            "policestation": ["null"]
        }], "kkkkk", [{
            "policestation": ["null"]
        }]]
    }], "Kieni West", [{
        "ward": "null"
    }], "Mathira East", [{
        "ward": ["Magutu", [{
            "policestation": ["Magutu", [{
                "arbitrator": ["null"]
            }], "Magutu ps", [{
                "arbitrator": ["null"]
            }], "Kagochi PS", [{
                "arbitrator": ["null"]
            }]]
        }], "Iriani", [{
            "policestation": ["null"]
        }], "Konyu", [{
            "policestation": ["null"]
        }], "Kirimukuyu", [{
            "policestation": ["null"]
        }], "Karatina Town", [{
            "policestation": ["Kagochi PS", [{
                "arbitrator": ["null"]
            }], "Gitunduti PS", [{
                "arbitrator": ["christopher bundi", "silvester kasanga"]
            }]]
        }]]
    }], "Mukurweini", [{
        "ward": "null"
    }], "Tetu", [{
        "ward": ["rulie", [{
            "policestation": ["null"]
        }]]
    }]]
 }

This is the code and trying to access the subcounties with excluding the keys to the wardslist

  try {
  JSONArray jsonArray = new JSONArray (response);

 for (int i = 0; i < jsonArray.length (); i++) {
   JSONObject jsonObject = jsonArray.getJSONObject (i);

  String subcounty = jsonObject.getString ("subcounty");
  Toast.makeText (getApplicationContext (), subcounty, Toast.LENGTH_LONG).show ();}//close try and catch follows

The challenge is to select the subcounty value exempting those values that are key to ward. Thank as you offer support

9
  • the JSON format is not correct, first element of your JSON array is string and second is a JSONArray Commented Mar 6, 2019 at 8:01
  • how should i structure the json to achieve the impression of ... counties under it listing of subcounties in it...then for every subcounty the listing of wards in it and so on . Thank you as you offer the support Commented Mar 6, 2019 at 8:05
  • you can add Json object inside Json array and 1 son object can contain name:"country name", sub:[array of sub countries] e.g [{"name":"Kieni East", "ward": ["sub1","sub2","sub3"] },{"name":"Kieni West", "ward": ["subWest1","subWest2","subWest3"] }] Commented Mar 6, 2019 at 9:18
  • I hope you know how JSON works, each {} represents an object and [] represents array. objects contains value pairs i.e key:value. for more details w3schools.com/js/js_json_intro.asp Commented Mar 6, 2019 at 9:24
  • Thank you sir hfarhanahmed am working on such structure and i will give the feedback Commented Mar 6, 2019 at 9:46

2 Answers 2

0

Use the following code and JsonFormat, I hope this will help.

String response = "[{"name":"Country 1", "subs": ["sub1","sub2","sub3"] },{"name":"Country 2", "subs": ["subWest1","subWest2","subWest3"] }]";
        try{
            JSONArray mainList = new JSONArray(response);
            ArrayList<String>   countries   =   new ArrayList<>();
            ArrayList<String>   subs   =   new ArrayList<>();

            for (int a=0;a<mainList.length();a++){
                JSONObject object=mainList.getJSONObject(a);
                countries.add(object.getString("name"));
                subs.add(object.getString("subs"));
            }

            setAdapterForCountries(countries);
        }catch(JSONException e){
            e.printStackTrace();
        }

on Item select callback do this

////On Item Select
            JSONArray   subCountries    =   new JSONArray(subs.get(selectedItemPosition));
            setSubCountiresAdapter(subCountries);

This is how you get a string list from that JSONArray.

private void setSubCountiresAdapter(JSONArray   subs){
    ArrayList<String>   subsNames   =   new ArrayList<>();
    for (int a=0;a<subs.length();a++){
        try {
            subsNames.add(subs.getString(a));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

By revising the array into correct format i.e [{ "county": "Mombasa", "subcounty": [{ "subcountyname": "mtwapa", "ward": [{ "wardname": "funny", "policestation": [{ "stationame": "Magutu ps", "arbitrator": [{ "arbitratorname": "christopher bundi" }] }] }] }, { "subcountyname": "likoni", "ward": [{ "wardname": "null" }] }, { "subcountyname": "hihi", "ward": [{ "wardname": "null" }] }, { "subcountyname": "mambo", "ward": [{ "wardname": "null" }] }] }, { "county": "Kwale", "subcounty": [{ "subcountyname": "null" }] }] then i can access data from the array by looping through the objects in initial array. the at this level with the object subcounty i can get the array in it,,,the get the object subcounty name in that array and so on for better clarity refer Parsing JSON array and object in Android

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.