user3264924 user3264924 - 4 years ago 114
JSON Question

How to obtain and convert custom json data as strings in android

I've googled around and found many tutorials(duplicates) and tips about json for android, but I find it difficult to perceive. I find it hard to get the score and the names as strings from the following json that I've retrieved from my database. I tried to get the result object first and get the names and scores but not certain how I can get manage to get it from [{},{}].

Are there some easy examples or tips? It sounds silly, but I need your help. I would like to hear from you!

{
"result": [
{
"id": "3",
"name": "Bobby",
"score": "44"
},
{
"id": "2",
"name": "Mike",
"score": "10"
}
]
}

Answer Source

Let,

String s = "{"result": [{"id": "3","name": "Bobby","score": "44"},{"id": "2","name": "Mike","score": "10"}]}";  

JSONObject jsonObject = new JSONObject(s);
JSONArray result= jsonObject .getJSONArray("result");

for(int i = 0; i < result.length(); i++) {
    JSONObject json = result.getJSONObject(i);
    String name = json.getString("name");
    String score = json.getString("score");
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download