Parsing JSON string in android

15,086

Solution 1

In this case you can use the keys method of the JSONObject class. It will basically returns an Iterator of the keys, that you can then iterate to get and put the values in a map:

try {
    JSONObject jsonObject = new JSONObject(theJsonString);
    Iterator keys = jsonObject.keys();
    Map<String, String> map = new HashMap<String, String>();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        map.put(key, jsonObject.getString(key));
    }
    System.out.println(map);// this map will contain your json stuff
} catch (JSONException e) {
    e.printStackTrace();
}

Solution 2

Note that Jackson can deserialize either of the two JSON examples very simply.

ObjectMapper mapper = new ObjectMapper();
Map<String, Object> values = mapper.readValue(theJsonString, Map.class);

Gson can do it similarly simply if you're satisfied with a Map<String, String>, instead of a Map<String, Object>. Currently, Gson would need custom deserialization for a Map<String, Object>.

Share:
15,086
Admin
Author by

Admin

Updated on June 16, 2022

Comments

  • Admin
    Admin almost 2 years

    Parsing JSON seems like it is a pretty common topic of discussion on here. I have looked around and still have not found what I am looking for. Here is my code for my HttpClient

    public class CreateJsonRequest {
    
    
        public static String SendJsonRequest(String URL, Map<String,Object> params){
                try{
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(URL);
    
                    JSONObject holder = new JSONObject();
    
                    for (Map.Entry<String, Object> m : params.entrySet()){
                        try {
                            holder.put(m.getKey(), m.getValue());
                        }
                        catch (JSONException e) {
                            Log.e("Hmmmm", "JSONException : "+e);
                        }
                    }   
                    StringEntity se;
                    se = new StringEntity(holder.toString());
    
                    httpPost.setEntity(se);
                    httpPost.setHeader("Accept", "text/json");
                    httpPost.setHeader("Content-type", "text/json");
    
                    HttpResponse response = httpClient.execute(httpPost);
                    HttpEntity entity = response.getEntity();
    
                    if(entity != null){
                        final JSONObject respObject = new JSONObject(EntityUtils.toString(entity));
                        String result = respObject.toString();      
                        parseJSON(result);
    

    I am using a HttpClient to send a JSON request to a server. The server then returns a response in JSON. This works great. Now here is where I am running into trouble. I am receiving an HttpEntity from the server. I am then turning that into a string which looks like this. {"Make":"Ford","Year": 1975, "Model":"Mustang"} I want to be able to send this string to my parseJSON(String jString) method and it return a key value map. Where I feel this differs from other posts is that I want the parse method to be able to create a key value map for any JSON string I send it. So if I sent it {"Engine":"v8","Cylinders": 8, "Transmission":"Manual","Gears": 4} it would still work. Is this doable? And if so, could you give me some nudges in the right direction?

  • Admin
    Admin almost 13 years
    Ah...I didn't even think about that. All of the examples I saw were using known keys...yes that should work. I'll try it in a bit. Thanks!
  • Admin
    Admin almost 13 years
    Worked perfectly. Thank you very much.