Converting JSONarray to ArrayList
我正在下载JSON字符串并将其转换为JSONArray。 我将其放入列表视图中,以后需要从该列表视图中删除,并且由于JSONArray没有.remove方法(感谢Obama),因此我尝试将其转换为arraylist。
这是我的JSON(array.toString()):
1 | [{"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"}] |
我需要将其放入数组,并能够通过它们各自的键来调用字符串。 感谢任何帮助!
1 2 3 4 5 6 7 | 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)); } } |
我已经使用
将以下行添加到模块的
1 2 3 4 5 6 | dependencies { // ... // Note that `compile` will be deprecated. Use `implementation` instead. // See https://stackoverflow.com/a/44409111 for more info implementation 'com.google.code.gson:gson:2.8.2' } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | private String jsonString ="[ " + " { " + " "id": "c200", " + " "name": "Ravi Tamada", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "male", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " }, " + " { " + " "id": "c201", " + " "name": "Johnny Depp", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "male", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " }, " + " { " + " "id": "c202", " + " "name": "Leonardo Dicaprio", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "male", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " }, " + " { " + " "id": "c203", " + " "name": "John Wayne", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "male", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " }, " + " { " + " "id": "c204", " + " "name": "Angelina Jolie", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "female", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " }, " + " { " + " "id": "c205", " + " "name": "Dido", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "female", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " }, " + " { " + " "id": "c206", " + " "name": "Adele", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "female", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " }, " + " { " + " "id": "c207", " + " "name": "Hugh Jackman", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "male", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " }, " + " { " + " "id": "c208", " + " "name": "Will Smith", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "male", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " }, " + " { " + " "id": "c209", " + " "name": "Clint Eastwood", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "male", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " }, " + " { " + " "id": "c2010", " + " "name": "Barack Obama", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "male", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " }, " + " { " + " "id": "c2011", " + " "name": "Kate Winslet", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "female", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " }, " + " { " + " "id": "c2012", " + " "name": "Eminem", " + " "email": "[email protected]", " + " "address": "xx-xx-xxxx,x - street, x - country", " + " "gender" : "male", " + " "phone": { " + " "mobile": "+91 0000000000", " + " "home": "00 000000", " + " "office": "00 000000" " + " } " + " } " + " ]"; |
1 2 3 4 5 | public class ContactModel { public String id; public String name; public String email; } |
用于将JSON字符串转换为
注意:您必须导入
1 2 3 4 5 6 7 8 9 10 11 12 13 | // Top of file import java.lang.reflect.Type; // ... private void parseJSON() { 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); } } |
希望这会帮助你。
我有快速的解决方案。只需创建一个文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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); } } |
用法:
1 | ArrayList<Object> list = ArrayUtil.convert(jArray); |
要么
1 | JSONArray jArr = ArrayUtil.convert(list); |
无需将JSON字符串转换为ArrayList甚至是Map,您只需创建一个JSONObject本身即可。该对象可以根据需要通过键获取字符串值,也可以删除对象。
要从格式正确的JSON字符串创建
1 | JSONObject json = new JSONObject(jsonString); |
尝试这种方式
只需循环遍历,即可构建自己的数组。这段代码假定它是一个字符串数组,不难修改以适合您的特定数组结构。
1 2 3 4 | JSONArray jsonArray = new JSONArray(jsonArrayString); List<String> list = new ArrayList<String>(); for (int i=0; i<jsonArray.length(); i++) { list.add( jsonArray.getString(i) ); |
在Java 8中,
1 | IntStream.range(0,jsonArray.length()).mapToObj(i->jsonArray.getString(i)).collect(Collectors.toList()) |
1 2 3 | JSONArray array = new JSONArray(json); List<JSONObject> list = new ArrayList(); for (int i = 0; i < array.length();list.add(array.getJSONObject(i++))); |
为方便起见,请使用POJO。
尝试这样..
1 2 3 4 5 6 7 8 9 | List<YourPojoObject> yourPojos = new ArrayList<YourPojoObject>(); JSONObject jsonObject = new JSONObject(jsonString); YourPojoObject yourPojo = new YourPojoObject(); yourPojo.setId(jsonObject.getString("idName")); ... ... yourPojos.add(yourPojo); |
我有快速的解决方案。只需创建一个文件
1 2 | ObjectMapper mapper = new ObjectMapper(); List<Student> list = Arrays.asList(mapper.readValue(jsonString, Student[].class)); |
用法:
1 | ArrayList<Object> list = ArrayUtil.convert(jArray); |
要么
1 | JSONArray jArr = ArrayUtil.convert(list); |
使用Gson
1 2 3 4 5 6 7 | List<Student> students = new ArrayList<>(); JSONArray jsonArray = new JSONArray(stringJsonContainArray); for (int i = 0; i < jsonArray.length(); i++) { Student student = new Gson().fromJson(jsonArray.get(i).toString(), Student.class); students.add(student); } return students; |
通用变体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | private interface ParseCallable< T > { T call(JSONArray jsonArray, int index) throws JSONException; } private < T > List< T > getList(JSONArray jsonArray, ParseCallable< T > callable) throws Exception { List< T > list = new ArrayList<>(jsonArray.length()); for (int i = 0; i < jsonArray.length(); i++) { T t = callable.call(jsonArray, i); list.add(t); } return list; } |
用法
1 2 3 4 5 6 7 8 9 10 11 12 13 | List<String> listKeyString = getList(dataJsonObject.getJSONArray("keyString"), new ParseCallable<String>() { @Override public String call(JSONArray jsonArray, int index) throws JSONException { return jsonArray.getString(index); } }); List<Integer> listKeyInteger = getList(dataJsonObject.getJSONArray("keyInteger"), new ParseCallable<Integer>() { @Override public Integer call(JSONArray jsonArray, int index) throws JSONException { return jsonArray.getInt(index); } }); |
1 2 3 4 5 6 7 8 9 | 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; } |