-1

I want to make an array with json string,But don't know... if my json code in my url is:

   {
 "status":[
{ "id": "id_1", "name": "name_1", "city": "aa" } ,
{ "id": "id_2", "name": "name_2", "city": "bb" } ,
{ "id": "id_3", "name": "name_3", "city": "cc" } ,
{ "id": "id_4", "name": "name_4", "city": "ee" } 
]
   }

and I want to make to this array:

id = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
name = new String[] { "name_1", "name_2", "name_3", "name_4", "name_5", "name_6", "name_7", "name_8", "name_9", "name_10" };

please HELP ME.TNX U

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
sob han
  • 39
  • 6
  • 1
    Search on _Google_ how to parse JSON? – M D Jul 16 '15 at 11:48
  • possible duplicate of [Java convert a Json string to an array](http://stackoverflow.com/questions/11527214/java-convert-a-json-string-to-an-array) – Pankaj Jul 16 '15 at 11:51

2 Answers2

1

Create an entity class

class Model{
    public String id,name,city;
}

and then parse the json string by this code

    // note yourString below used is the json string which you are mentioned in the question
    ArrayList<Model> list = new ArrayList<Model>();
    JSONObject jobj = new JSONObject(yourString);
    JSONArray array = jobj.getJSONArray("status");
    for(int i =0;i<array.length();i++){
        JSONObject temp = array.getJSONObject(i);
        Model model = new Model();
        model.id = temp.getString("id_1");
        model.name = temp.getString("name_1");
        model.city = temp.getString("city");
        list.add(model);
    }
}
Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
0

public class Sample { ArrayList status;

public class Model {
    public String id, name, city;

}

}

Nitin Zagade
  • 71
  • 1
  • 7