java String字符串怎么转换成List,还是说先转换成json,再由json转换成list

这是在服务器上获取的String类型字符串
{"code":"1","table":{"title":["单据名称","单据日期","提交人员","单据流水","帐套编号","帐套名称","逻辑表名"],"value0":["采购请购单","20160124","0034","86","005","演示","CGQGD"],"value1":["采购请购单","20160123","0056","78","005","演示","CGQGD"],"value2":["采购请购单","20160123","0041","80","005","演示","CGQGD"],"value3":["采购请购单","20160122","0037","81","005","演示","CGQGD"],"value4":["采购请购单","20160122","0052","82","005","演示","CGQGD"],"value5":["采购请购单","20160122","0043","83","005","演示","CGQGD"],"value6":["采购请购单","20160124","0034","84","005","演示","CGQGD"],"value7":["采购请购单","20160126","0054","88","005","演示","CGQGD"]}}
我先把他转换成list然后发送到jsp页面,给他遍历用表格显示出来,我该怎么便利,网上转换成list的方法,我用这不行。
最新回答
微浅笑°

2024-11-03 02:24:50

由于你这种字符串是JSON格式的字符串,所以要转成LIST,首先要先转成JSON格式的数据。

你可以下一个jar包,com.alibaba.json,或者其他json工具包,调用工具包里面的类似:json.parseJson(string); 这种方法转换成Json,然后把里面的数据用JSON的形式读取出来,大概方法是:json.get();

然后有了读取的数据,就可以随意操作了,想怎么转换怎么转换。
追问
可不可以加Q聊一下
释放

2024-11-03 03:03:26

服务端:
//获取数据
List<City> list = getCities();
//将数据转成JSON字符串
JSONArray jsonObj =
JSONArray.fromObject(list);
System.out.println(jsonObj);//控制台显示
String jsonStr = jsonObj.toString();
out.print(jsonStr);//给Ajax对象返回
out.close();
客户端:
var xhr = createXhr();
xhr.open("get","loadcities.do",true);
//注册回调处理
xhr.onreadystatechange = function(){
if(xhr.readyState==4&&xhr.status==200){
//获取服务器返回的信息(JSON对象数组字符串)
var data = xhr.responseText;
//将字符串转成JSON对象数组类型
var json_data = JSON.parse(data);
//循环JSON对象数组
var city_select =
document.getElementById("city");
for(var i=0;i<json_data.length;i++){
//json_data[i]表示{"id":1,"name":"北京"}
var id = json_data[i].id;//获取id值
var name = json_data[i].name;//获取name值
//创建一个<option>元素
var opt = new Option(name,id);
//添加到<select>元素中
city_select.options[i] = opt;
}
}