Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
124 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...