在Android问题中解析Json String

时间:2014-12-13 10:51:22

标签: android json

我有这个参数的Json String:

  

{名称,颜色,标题,记述,技能,电话,城市,照片,, visiting_id,visitings:{visiting_id:状态}}

我得到了这个答复:

 {
  "status": "SUCCESS",
  "error": "",
  "class": "contact",
  "action": "getContactInfo",
  "code": 200,
  "body": {
    "title": "Android Develooer",
    "color": "2",
    "phone": "1234567890",
    "city": "Kyiv, UA",
    "visiting_id": "35",
    "photo": "statics\/images\/user\/21417805976.3776.jpeg",
    "descrip": "divsjcsufjv shgsvjcs",
    "name": "\u0412\u0438\u0442\u0430\u043b\u0438\u0439 \u0420\u043e\u0433\u043e\u0432\u043e\u0439",
    "skills": "Web, Ios, Android, Design",
    "visitings": {
      "42": "new",
      "44": "new",
      "46": "new"
    }
  }
}

最后一个元素是一个包含元素的列表。每个都有两个参数:id和state。 什么是在Android中解析此列表的麻烦方式?

1 个答案:

答案 0 :(得分:1)

您可以将这些值转换为Map<String,String>,其中key是您的idvaluestatus

代码:

 try {
        JSONObject object = new JSONObject(jsonString);
        JSONObject bodyObject = object.getJSONObject("body");
        JSONObject visitingsObject = bodyObject.getJSONObject("visitings");
        Iterator<String> keys = visitingsObject.keys();
        Map<String, String> map = new HashMap<String, String>();
        while (keys.hasNext()) {
            String key = keys.next();
            String value = visitingsObject.getString(key);
            map.put(key, value);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }