大佬教程收集整理的这篇文章主要介绍了记一次fastJson调错,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
项目需要将数据保存成json 再解析,于是有了下面的正确方法
@Test
public void testJsonArray()
{
JSONObject json = new JSONObject();
List<TT> list = new ArrayList<TT>();
TT t = new TT();
t.setStation("武汉");
t.setTimes(2);
list.add(t);
t = new TT();
t.setStation("包头");
t.setTimes(20);
list.add(t);
json.put("data",list);
try
{
String jjstr = json.toJSONString();
JSONObject tempJson = JSONObject.parSEObject(jjstr);
System.out.println(jjstr);
String temp = tempJson.get("data").toString();
System.out.println(temp);
List<TT> l = JSONArray.parseArray(temp,TT.class);
System.out.println(l);
}
catch (Exception E)
{
System.out.println("error first");
}
}
注意中间 的jjstr 为一个中转String,如果不需要这个,如下面的错误范例,
@Test
public void testJsonArray()
{
JSONObject json = new JSONObject();
List<TT> list = new ArrayList<TT>();
TT t = new TT();
t.setStation("武汉");
t.setTimes(2);
list.add(t);
t = new TT();
t.setStation("包头");
t.setTimes(20);
list.add(t);
json.put("data",list);
try
{
String temp = json.get("data").toString();
System.out.println(temp);
List<TT> l = JSONArray.parseArray(temp,TT.class);
System.out.println(l);
}
catch (Exception E)
{
System.out.println("error first");
}
}
以上是大佬教程为你收集整理的记一次fastJson调错全部内容,希望文章能够帮你解决记一次fastJson调错所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。