如何将 JSON 字符串转换为对象的 Arraylist
Posted
技术标签:
【中文标题】如何将 JSON 字符串转换为对象的 Arraylist【英文标题】:How to convert JSON String into Arraylist of Objects 【发布时间】:2018-10-26 12:09:49 【问题描述】:我有 JSON 字符串,从 HTTP 请求接收到:
[
"id":15,
"title":"1",
"description":"desc",
"user_id":152
,
"id":18,
"title":"2",
"description":"desc",
"user_id":152
,
"id":19,
"title":"tab3",
"description":"zadanka",
"user_id":152
]
如何将其转换为对象的 ArrayList?
【问题讨论】:
【参考方案1】:你需要声明一个pojo
class Data
String id;
String title;
String description;
String userId;
//Generate setter an getter
对 json 的迭代如下:
JSONArray jsonArr = new JSONArray("[your JSON Stirng]");
List<Data> dataList = new ArrayList<Data>();
for (int i = 0; i < jsonArr.length(); i++)
JSONObject jsonObj = jsonArr.getJSONObject(i);
Data data = new Data();
data.setId(jsonObj.getString("id"));
data.setTitle(jsonObj.getString("title"));
data.setDescription(jsonObj.getString("description"));
data.setUserId(jsonObj.getString("user_id"));
dataList.add(data);
你还需要 json jar。可以从here下载
【讨论】:
谢谢。这正是我所需要的。 不要从海报提供的链接下载,而是使用 Maven 和合法来源:mvnrepository.com/artifact/org.json/json【参考方案2】:使用 Gson
Gson gson = new Gson();
ArrayList<Object> listFromGson = gson.fromJson("json string",
new TypeToken<ArrayList<Object>>() .getType());
使用杰克逊
ObjectMapper mapper = new ObjectMapper();
ArrayList<Object> listFromJackson = mapper.readValue("json string",
new TypeReference<ArrayList<Object>>());
如果你可以将 pojo 定义为
public class Example
private Integer id;
private String title;
private String description;
private Integer userId;
// setters / getters
然后
ArrayList<Example> listFromGson = gson.fromJson("json string",
new TypeToken<ArrayList<Example>>() .getType());
ArrayList<Example> listFromJackson = mapper.readValue("json string",
new TypeReference<ArrayList<Example>>());
此外,您应该更喜欢使用List
而不是ArrayList
。
【讨论】:
【参考方案3】:如果您使用的是 RestApi,则将注释 @RequestBody 与您的 pojo 类一起使用。
@RequestMapping(value="/your api name", method=RequestMethod.POST)
public ResponseData createUser(@RequestBody MyPojo myPojo)
System.out.println("Creating User "+myPojo.toString());
//Here you will able to access your request data from myPojo object
制作你的 pojo 类:
public class MyPojo
private Data[] data;
public Data[] getData ()
return data;
public void setData (Data[] data)
this.data = data;
@Override
public String toString()
return "ClassPojo [data = "+data+"]";
public class Data
private String id;
private String title;
private String description;
private String user_id;
public String getId ()
return id;
public void setId (String id)
this.id = id;
public String getTitle ()
return title;
public void setTitle (String title)
this.title = title;
public String getDescription ()
return description;
public void setDescription (String description)
this.description = description;
public String getUser_id ()
return user_id;
public void setUser_id (String user_id)
this.user_id = user_id;
@Override
public String toString()
return "ClassPojo [id = "+id+", title = "+title+", description = "+description+", user_id = "+user_id+"]";
【讨论】:
【参考方案4】:除了@Sudhir,我推荐使用Gson
Gson gson = new GsonBuilder().create();
Data p = gson.fromJson(jsonString, Data.class);
// Or to array.
Data[] data = gson.fromJson(jsonString, Data[].class);
【讨论】:
以上是关于如何将 JSON 字符串转换为对象的 Arraylist的主要内容,如果未能解决你的问题,请参考以下文章