如何将Json字符串转换为android中的数组?

时间:2015-07-16 11:47:11

标签: android json

我想用json字符串创建一个数组,但不知道...... 如果我的网址中的json代码是:

   {
 "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" } 
]
   }

我想对这个数组做出:

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" };

请帮助ME.TNX U

2 个答案:

答案 0 :(得分:1)

创建实体类

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

然后通过此代码解析json字符串

    // 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);
    }
}

答案 1 :(得分:0)

公共类Sample {     ArrayList状态;

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

}

}