arinze
arinze

Reputation: 449

display single array json data in 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)

Upvotes: 3

Views: 685

Answers (4)

Jordi Castilla
Jordi Castilla

Reputation: 27001

Your method seems too much complex, appart, your problem is you need to read headers attribute before reading the other attributes.

Using com.google.gson.GSon

protected static void read() throws Exception {
    com.google.gson.Gson jParser = new com.google.gson.Gson();

    Reader r = new BufferedReader(new FileReader(new File(FILE_PATH)));

    // get all the object
    JsonObject json = jParser.fromJson(r, JsonObject.class);

    // get headers object
    JsonObject members = json.get("headers").getAsJsonObject();

    // get attributes inside headers!
    System.out.println(members.get("id"));
    System.out.println(members.get("name"));
    System.out.println(members.get("statusMessage"));
}

OUTPUT:

"4"
"GLO"
"Hi +234805, an ACCESS FEE of N20.00 will be charged in order to access this Platform"

Download here the jar files


What varable is FILE_PATH – arinze 3 mins ago

Is a path where I placed the file with content you put in your question, in my case:

String FILE_PATH = "D:\\Users\\jordi\\datos.txt"; 

but you must put your own path.... anyway, if you need get a Reader from an URL just use this example:

URL url = new URL("http://your-json-url");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

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();
}

Upvotes: 1

am110787
am110787

Reputation: 316

You can use Gson (Complete Tutorial) or Jackson (Complete Tutorial)

Upvotes: 0

Dexto
Dexto

Reputation: 1201

Try this

 try {
        JSONObject rootJsonObject = new JSONObject(response);
        if (rootJsonObject.getString("status").equalsIgnoreCase("SUCCESS")) {
            JSONObject headerJsonObject = new JSONObject(rootJsonObject.getString("headers"));
            String name = headerJsonObject.getString("name");
            String code = headerJsonObject.getString("code");
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

Upvotes: 0

Related Questions