[
{
"points": 411,
"type": "C"
},
{
"points": 1600,
"type": "G"
},
{
"points": 13540,
"type": "I"
}
]
I have this type of json from api
[
{
"points": 411,
"type": "C"
},
{
"points": 1600,
"type": "G"
},
{
"points": 13540,
"type": "I"
}
]
I have this type of json from api
Note:**Missing
{
in second array Data.....
[
{
"points": 411,
"type": "C"
},
{
"points": 1600,
"type": "G"
},
{
"points": 13540,
"type": "I"
}
]
Code
try {
JSONArray jsonArray=new JSONArray(Response);
for(int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject=jsonArray.getJSONObject(i);
int points=jsonObject.getInt("points");
String type=jsonObject.getString("type");
// if you have only three textview. and three array data than set as like that...
if(i=0)
{
textview1.setText(String.valueOf(points));
}
else if(i==1)
{
textview2.setText(String.valueOf(points));
}
else
{
textview3.setText(String.valueOf(points));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Anything inside [] are considered JSONArray Elements---> In this case, it is a JSONArray containing JSONObject
So let's say the JSON is something like :
{
abc: [
{"name":"kushan"}
]
}
you can parse it depending on what library you use to parse it with....
theres a GSON from google, there simple JSON,org.json etc...
basic scenario would be:
try{
JSONObject obj= new JSONObject("StringcontainingJSON");
JSONArray jsarr= obj.getJSONArray("abc");
JSONObject innerObj= jsarr.getJSONObject(0); //0th position is the only element of the array
String nameValue=innerObj.getString("name");
}catch(JSONException e){
//any key value duplicate or invalid key or missing etc.. will throw the exception
}