0

可能重复:
在 Android 中发送和解析 JSON

{
    "title": " '''Sachin Ramesh Tendulkar''' (born 24 April 1973) is an Indian cricketer widely regarded as one of the greatest batsmen in the history of cricket. ",
    "sub": {
        "sub0": {
            "name": "Cricket",
            "importance": "1"
        },
        "sub1": {
            "name": "Wisden Cricketers of the Year",
            "importance": "1"
        },
        "sub2": {
            "name": "Marathi people",
            "importance": "1"
        },

    },
    "heading": {
        "p1": {
            "point": " Tendulkar . "
        },
        "p2": {
            "point": " He."
        },
        "p3": {
            "point": " 2009. "
        },
    }
}
4

2 回答 2

2

每当从网络获取结果时,它都会返回一个字符串,您必须像这样将其转换为 JsonObject

JSONObject json = new JSONObject (response);

这样,您将获得整个字符串转换为 json,然后您可以使用 JsonObject 类方法从 json 中检索所有值,如下所示

String player name = json.getString("title");

你会得到这个结果

Sachin Ramesh Tendulkar'''(生于 1973 年 4 月 24 日)是一名印度板球运动员,被广泛认为是板球史上最伟大的击球手之一。

于 2012-05-17T06:57:48.193 回答
1

在这种情况下,您可以使用 JSONObject 类的 keys 方法。它基本上会返回一个键的迭代器,然后您可以迭代以获取并将值放入映射中:

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

参考:Parsing Json String , Parse Json String in Android

于 2012-05-17T06:57:58.553 回答