花了将近一个多小时来研究这个问题,几近崩溃。
解决方法:首先创建与你传过来的json数据name字段对应的实体类。
例[{"name":"1","id","1"},{"name":"2","id","2"}],那么你的实体类的私有变量就是name,id这两个
我的实体类:
package User;
public class type {
private String one;
private String two;
private String three;
private String four;
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
public String getThree() {
return three;
}
public void setThree(String three) {
this.three = three;
}
public String getFour() {
return four;
}
public void setFour(String four) {
this.four = four;
}
public type(String one, String two, String three, String four) {
super();
this.one = one;
this.two = two;
this.three = three;
this.four = four;
}
public type() {
super();
}
}
然后需要的就是在你的接口方法中所需要的转换(接口方法就是你前端请求的url地址,也可以抽成一个类 ,具体看你):
//获取传过来的json
String json=req.getParameter("json");
//将json字符串转换为json对象
JSONArray jsonArray=JSONArray.fromObject(json);
//转换为实体类的list集合
List list=(List) jsonArray.toCollection(jsonArray, type.class);
没错,这就已经完成了。你需要那个就调用哪个:例如:list.get(索引).get...(),这个get方法是你在实体类中写的。
可以输出一下查看成功没有。
标签:String,list,two,three,four,json,解析,public
来源: https://www.cnblogs.com/moxihuishou/p/13908483.html