-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

2
  • 1
    Search on Google how to parse JSON? Commented Jul 16, 2015 at 11:48
  • possible duplicate of Java convert a Json string to an array Commented Jul 16, 2015 at 11:51

2 Answers 2

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

Comments

0

public class Sample { ArrayList status;

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

}

}

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.