将JSONarray转换为ArrayList

我正在下载一个JSONstring并将其转换为JSONArray。 我把它放进一个列表视图,需要能够从列表视图中删除,因为JSONArray没有.remove方法(感谢奥巴马),我试图将其转换为一个ArrayList。

这里是我的JSON(array.toString()):

[{"thumb_url":"tb-1370913834.jpg","event_id":"15","count":"44","event_tagline":"this is a tagline","event_name":"5th birthday","event_end":"1370919600","event_start":"1370876400"}] 

我需要把它放到一个数组中,并能够通过各自的键调用string。 感谢任何帮助!

 ArrayList<String> listdata = new ArrayList<String>(); JSONArray jArray = (JSONArray)jsonObject; if (jArray != null) { for (int i=0;i<jArray.length();i++){ listdata.add(jArray.getString(i)); } } 

我已经使用GsonLibrary by Google )为CustomModel类完成了这个工作:

为什么使用这个代码:

  1. 快速处理
  2. 无需每个实体 编写Getter Setter方法 / 函数
  3. 短代码
  4. 容易明白

依赖关系build.gradle中添加compile 'com.google.code.gson:gson:2.2.4'

定义Jsonstring

 private String jsonString = "[\n" + " {\n" + " \"id\": \"c200\",\n" + " \"name\": \"Ravi Tamada\",\n" + " \"email\": \"ravi@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c201\",\n" + " \"name\": \"Johnny Depp\",\n" + " \"email\": \"johnny_depp@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c202\",\n" + " \"name\": \"Leonardo Dicaprio\",\n" + " \"email\": \"leonardo_dicaprio@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c203\",\n" + " \"name\": \"John Wayne\",\n" + " \"email\": \"john_wayne@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c204\",\n" + " \"name\": \"Angelina Jolie\",\n" + " \"email\": \"angelina_jolie@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"female\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c205\",\n" + " \"name\": \"Dido\",\n" + " \"email\": \"dido@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"female\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c206\",\n" + " \"name\": \"Adele\",\n" + " \"email\": \"adele@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"female\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c207\",\n" + " \"name\": \"Hugh Jackman\",\n" + " \"email\": \"hugh_jackman@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c208\",\n" + " \"name\": \"Will Smith\",\n" + " \"email\": \"will_smith@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c209\",\n" + " \"name\": \"Clint Eastwood\",\n" + " \"email\": \"clint_eastwood@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c2010\",\n" + " \"name\": \"Barack Obama\",\n" + " \"email\": \"barack_obama@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c2011\",\n" + " \"name\": \"Kate Winslet\",\n" + " \"email\": \"kate_winslet@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"female\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " },\n" + " {\n" + " \"id\": \"c2012\",\n" + " \"name\": \"Eminem\",\n" + " \"email\": \"eminem@gmail.com\",\n" + " \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" + " \"gender\" : \"male\",\n" + " \"phone\": {\n" + " \"mobile\": \"+91 0000000000\",\n" + " \"home\": \"00 000000\",\n" + " \"office\": \"00 000000\"\n" + " }\n" + " }\n" + " ]"; 

ContactModel.java

 public class ContactModel{ public String id; public String name; public String email; } 

JSONArray 转换ArrayList的代码:

你必须导入java.lang.reflect.Type;

 Gson gson = new Gson(); Type type = new TypeToken<List<ContactModel>>(){}.getType(); List<ContactModel> contactList = gson.fromJson(jsonString, type); for (ContactModel contact : contactList){ Log.i("Contact Details", contact.id + "-" + contact.name + "-" + contact.email); } 

希望这会帮助你。

而不是将JSONstring转换为一个ArrayList或一个Map,您可以创build一个JSONObject本身。 这个对象可以通过键来获取string值 ,也可以移除对象 。

要从格式正确的JSONstring创buildJSONObject ,只需调用相应的构造函数即可 。

 JSONObject json = new JSONObject(jsonString); 

尝试这种方式简单地循环,build立自己的数组。 这段代码假定它是一个string数组,不应该很难修改,以适应您的特定数组结构。

 JSONArray jsonArray = new JSONArray(jsonArrayString); List<String> list = new ArrayList<String>(); for (int i=0; i<jsonArray.length(); i++) { list.add( jsonArray.getString(i) ); 

我有快速的解决scheme。 只要创build一个文件ArrayUtil.java

 import java.util.ArrayList; import java.util.Collection; import org.json.JSONArray; import org.json.JSONException; public class ArrayUtil { public static ArrayList<Object> convert(JSONArray jArr) { ArrayList<Object> list = new ArrayList<Object>(); try { for (int i=0, l=jArr.length(); i<l; i++){ list.add(jArr.get(i)); } } catch (JSONException e) {} return list; } public static JSONArray convert(Collection<Object> list) { return new JSONArray(list); } } 

用法:

 ArrayList<Object> list = ArrayUtil.convert(jArray); 

要么

 JSONArray jArr = ArrayUtil.convert(list); 
  JSONArray array = new JSONArray(json); List<JSONObject> list = new ArrayList(); for (int i = 0; i < array.length();list.add(array.getJSONObject(i++))); 

为了方便使用,使用POJO。

像这样试试

 List<YourPojoObject> yourPojos = new ArrayList<YourPojoObject>(); JSONObject jsonObject = new JSONObject(jsonString); YourPojoObject yourPojo = new YourPojoObject(); yourPojo.setId(jsonObject.getString("idName")); ... ... yourPojos.add(yourPojo); 

在Java 8中,

 IntStream.range(0,jsonArray.length()).mapToObj(i->jsonArray.getString(i)).collect(Collectors.toList()) 
 public static List<JSONObject> getJSONObjectListFromJSONArray(JSONArray array) throws JSONException { ArrayList<JSONObject> jsonObjects = new ArrayList<>(); for (int i = 0; i < (array != null ? array.length() : 0); jsonObjects.add(array.getJSONObject(i++)) ); return jsonObjects; } 
 ArrayList<String> dataList = new ArrayList<String>(); JSONArray jsonArray = (JSONArray)jsonObject; for(Object obj : jsonArray){ dataList.add((String)obj); }