Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display single array json data in android?

Tags:

json

android

i am trying to display json data in an android application but i have having difficulty i think is may have to do with the way the json file was formated .i want to get the value of name and code in the headers array. and it is not working

this is the json file

 {
"status": "SUCCESS",
"headers": 
{
"id": "4",
"name": "GLO",
"code": "GLO",
"background_color_code": "15B709",
"text_color_code": "ffffff",
"statusMessage": "Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"
},
"statusMessage": "Movies Loaded successfully",
"caption": "Discover"
}

this is the java code

 protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {

            ArrayList<HashMap<String, String>> categoryList = new ArrayList<HashMap<String, String>>();
            jParser = new JSONParser();

            JSONObject json = jParser.getJSONFromUrl(URL_CATEGORY);

            try {
                  JSONObject categories =json.getJSONObject("headers");

                   String state = categories.getString("name");
                    String status = categories.getString("code");

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_PIC, state);
                    map.put(TAG_NOTE, status);
                    categoryList.add(map);


            }catch (Throwable e){
                e.printStackTrace();
            }
            return categoryList;
        }

this is the error

07-25 11:05:50.766  15683-15697/com.example.cann I/System.out﹕ close [socket][/10.187.206.124:36118]
07-25 11:05:50.781  15683-15702/com.example.cann W/System.err﹕ org.json.JSONException: Value  at headers of type java.lang.String cannot be converted to JSONObject
07-25 11:05:50.782  15683-15702/com.example.cann W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:100)
07-25 11:05:50.782  15683-15702/com.example.cann W/System.err﹕ at org.json.JSONObject.getJSONObject(JSONObject.java:613)
07-25 11:05:50.782  15683-15702/com.example.cann W/System.err﹕ at com.example.cann.CategoryActivity$LoadComments.doInBackground(CategoryActivity.java:81)
07-25 11:05:50.782  15683-15702/com.example.cann W/System.err﹕ at com.example.cann.CategoryActivity$LoadComments.doInBackground(CategoryActivity.java:60)
like image 772
arinze Avatar asked Mar 04 '26 03:03

arinze


1 Answers

You can try with

try {
    JSONObject reader = new JSONObject(Your_URL);
    JSONArray jsonArray = reader.getJSONArray("headers");

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject e = jsonArray.getJSONObject(i);
        String name= e.getString("name");
    }
} catch (JSONException e) {
    e.printStackTrace();
}
like image 90
IntelliJ Amiya Avatar answered Mar 05 '26 17:03

IntelliJ Amiya