-4
[
  {

    "points": 411,
    "type": "C"
  },

  {

    "points": 1600,
    "type": "G"
  },

  {

    "points": 13540,
    "type": "I"
  }
]

I have this type of json from api

Md Sufi Khan
  • 1,751
  • 1
  • 14
  • 19

2 Answers2

0

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();
    }
Arjun saini
  • 4,223
  • 3
  • 23
  • 51
0

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
}
Kushan
  • 5,855
  • 3
  • 31
  • 45