0

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?

6
  • what are different type of visitings i.e (new ....)??? Commented Dec 13, 2014 at 11:00
  • I don't know. "new" - is a status and this field can be new/requested/approval/accepted. Commented Dec 13, 2014 at 11:05
  • i suggest visiting should be array and also revers this pair "42": "new" to "new": "24" Commented Dec 13, 2014 at 11:07
  • The first string you posted isn't valid JSON. Do you understand JSON? If not then I suggest you go to json.org where you can learn everything you need on just one page. As for parsing it, just look at the Android refernce docs for JSONObject. Commented Dec 13, 2014 at 11:09
  • First string just shows parameters that i should parse? but it is not current Json. Commented Dec 13, 2014 at 11:25

1 Answer 1

1

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.