我们如何解析这个json数据?

时间:2012-05-17 06:46:47

标签: java android json parsing

  

可能重复:
  Sending and Parsing JSON in Android

{
    "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. "
        },
    }
}

2 个答案:

答案 0 :(得分:2)

每当从web获得结果时,它返回一个字符串,你必须像这样将它转换为JsonObject

JSONObject json = new JSONObject (response);

通过这个你将获得整个字符串转换为json然后你可以使用像这样的JsonObject类方法从json中检索所有值

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

您将获得此结果

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

答案 1 :(得分: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 StringParse Json String in Android