我正在下载一个json字符串并将其转换为jsonarray。我把它放到一个listview中,以后需要能够从该listview中删除,而且由于jsonarray没有.remove方法(谢谢obama),我正在尝试将它转换为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"}]
我需要把它放到一个数组中,并且能够通过它们各自的键调用字符串。谢谢你的帮助!
17条答案
按热度按时间6za6bjd01#
java 8样式
icomxhvb2#
更简单的java 8替代方案:
rslzwgfq3#
试着用这种方法简单地循环,构建你自己的数组。这段代码假设它是一个字符串数组,修改它以适应特定的数组结构应该不难。
jljoyd4f4#
通用变型
v440hwme5#
@简化
kq4fsx7k6#
如果您想从json字符串数组中提取数据,下面是我的工作代码。将参数更改为您的数据。
pojo类
使用下面的代码行提取数据
它将返回pojo类对象的列表。
yqyhoc1h7#
为了方便起见,可以使用pojo。
像这样试试。。
deyfvvtc8#
q1qsirdb9#
t30tvxxf10#
使用gson
ss2ws0br11#
只是从主题出发:
将jsonarray转换为list(此处使用jackson jsonarray和object mapper):
myss37ts12#
ncgqoxb013#
在java 8中,
68bkxrlz14#
我有快速解决办法。只是创建一个文件
ArrayUtil.java
```import java.util.ArrayList;
import java.util.Collection;
import org.json.JSONArray;
import org.json.JSONException;
public class ArrayUtil
{
public static ArrayList convert(JSONArray jArr)
{
ArrayList list = new ArrayList();
try {
for (int i=0, l=jArr.length(); i<l; i++){
list.add(jArr.get(i));
}
} catch (JSONException e) {}
}
ArrayList list = ArrayUtil.convert(jArray);
JSONArray jArr = ArrayUtil.convert(list);
lokaqttq15#
我用的是
Gson
(由谷歌提供)。将以下行添加到模块的
build.gradle
: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
:将json字符串转换为
ArrayList<Model>
:注意:您必须导入
java.lang.reflect.Type;
:希望这对你有帮助。