user_android_dev
user_android_dev

Reputation: 99

Parse Json String in Android issue

I have Json String with this parameters:

{name,color,title,descrip,skills,phone,city,photo,,visiting_id,visitings:{visiting_id:status}}

And i get this response:

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

The last element is a list with elements. Each has two parameters: id and state. What is the bast way to parse this list in Android?

Upvotes: 0

Views: 72

Answers (1)

gio
gio

Reputation: 5020

You can convert those values into Map<String,String> where key is your id and value is status.

Code:

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

Upvotes: 1

Related Questions