3

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)
arinze
  • 439
  • 1
  • 6
  • 27

4 Answers4

1

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();
}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

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();
    }
Dexto
  • 1,151
  • 9
  • 18
  • where does the response varable come from – arinze Jul 25 '16 at 10:59
  • that is the server response in string – Dexto Jul 25 '16 at 11:02
  • this is what i am getting 07-25 12:10:49.206 28912-28926/? W/System.err﹕ org.json.JSONException: End of input at character 0 of 07-25 12:10:49.208 28912-28926/? W/System.err﹕ at org.json.JSONTokener.syntaxError(JSONTokener.java:449) – arinze Jul 25 '16 at 11:11
  • JSONObject json = jParser.getJSONFromUrl(URL_CATEGORY);String response = json.toString(); – Dexto Jul 25 '16 at 11:18
0

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

am110787
  • 316
  • 1
  • 2
  • 9
0

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));
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109