-3

How can I parse this JSON array in android?

Photograph of JSON

This is my android code:

private Boolean parseData(){
    try{

        JSONArray ja = new JSONArray(jsonData);
        JSONObject jo = null;

        wantedLists.clear();
        WantedList wantedList;

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

            jo = ja.getJSONObject(i);
            String lastname=jo.getString("lastname");
            String alyas=jo.getString("alyas");
            String reward=jo.getString("reward");
            String authority=jo.getString("authority");
            String location_name=jo.getString("location_name");
            String firstname=jo.getString( "firstname" );
            String middlename=jo.getString( "middlename" );
            String imageUrl=jo.getString("url");
            wantedList = new WantedList();

            wantedList.setLastname(lastname);
            wantedList.setAlyas(alyas);
            wantedList.setAuthority(authority);
            wantedList.setReward(reward);
            wantedList.setLocation_name(location_name);
            wantedList.setFirstname( firstname );
            wantedList.setMiddlename( middlename );
            wantedList.setImageUrl(imageUrl);

           wantedLists.add(wantedList);
        }
    }

Using the above code, the array is not parsed, can someone spot the bug?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • check this link http://stackoverflow.com/questions/18977144/how-to-parse-json-array-not-json-object-in-android – darwin Oct 15 '16 at 08:07
  • What is your expected result? Normally in PHP you would just use json decode it if you need to access individual values. i.e. `json_decode($string, true)` – DrewT Oct 15 '16 at 08:07
  • sorry Iam really new in this page i dont know how to post the problem i just want to parse the json data so i cant view it on my android application. how can i parse it? – Jzon Charles Oct 15 '16 at 08:12
  • are you getting any error on using above code?? – Sanjeet Oct 15 '16 at 08:52
  • i dont get any error but i just cant get the data to android studio when i run my program the response is unable to parse data – Jzon Charles Oct 15 '16 at 09:07
  • try logging and reading the error inside the `catch(Exception e)` block (which you should also include in the posted code), there's probably some info there on why the JSON can't be parsed – Gino Mempin Oct 15 '16 at 09:23
  • Iam really sorry I just started to post in this page just this day so i dont know what to do here thank you for informing me – Jzon Charles Oct 15 '16 at 09:32

1 Answers1

0

Your JSON uses middlename with an uppercase 'M':

"Middlename": "Lumbres",

But your JSON parser uses middlename with a lowercase 'm':

String middlename=jo.getString( "middlename" );

That should throw a org.json.JSONException: No value for middlename error.

Either modify your app's JSON parser to get "Middlename" or modify the JSON response (if you control the server or module that spits out the response) to use "middlename".

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135