html中在json数据取出一个list,在后台解析前端传递的json到list中

花了将近一个多小时来研究这个问题,几近崩溃。

解决方法:首先创建与你传过来的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值