解析JSON字符串中的android [英] Parsing JSON string in android

查看:91
本文介绍了解析JSON字符串中的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解析JSON好像它是在这里pretty的共同讨论的话题。我环顾四周,仍然没有找到我所期待的。 这是我的$ C $下我的HttpClient

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

我使用的是HttpClient的发送JSON请求到服务器。然后,服务器返回的JSON响应。这个伟大的工程。现在,这里是我遇到的麻烦。 我收到来自服务器的HttpEntity。我再打开这为一个字符串,它看起来像这样。 {做:福特,年:1975年,模式:野马} 我希望能够以这个字符串发送给我的 parseJSON(字符串的jstring)方法,它返回一个键值地图。在那里我感觉这不同于其他职位是我想要的解析方法能够创建一个键值地图的任何JSON字符串我送它。所以,如果我把它 {引擎:V8,缸:8,传输:手动,机器:4} 也算工作。这是可行的?如果是的话,你能不能给我一些轻推在正确的方向?

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?

推荐答案

在这种情况下,你可以使用的方法的JSONObject 类。它基本上会返回一个迭代的键,你就可以迭代来获得,并把值映射:

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

这篇关于解析JSON字符串中的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆