记一次fastJson调错
发布时间:2020-12-16 18:51:05 所属栏目:百科 来源:网络整理
导读:项目需要将数据保存成json 再解析,于是有了下面的正确方法 @Testpublic void testJsonArray(){ JSONObject json = new JSONObject() ; ListTT list = new ArrayListTT() ; TT t = new TT() ; t .setStation ( "武汉" ) ; t .setTimes ( 2 ) ; list .add (t)
项目需要将数据保存成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");
}
}
会报一个很奇怪的错误,尝试了很多办法,最后才想到把他搞个中间String ,模拟从库中取到的数据 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- IAP(In Application Programming)在线应用编程
- ORACLE 11g 使用ROWNUM完美解决ORA-00600:内部错误代码
- Swift中的数据存储
- activity_main.xml: java.lang.NullPointerException
- tdd – 依赖于数据的BDD测试的最佳策略是什么
- ajax提交整个form
- ruby-on-rails – 在不更改系统时钟的情况下,在rspec测试中
- 使用org.json lib的Java对象到JSON
- namespace-organization – 在c#中的不同命名空间中使用具有
- Oracle Net Manager 服务命名配置以及用PL/SQL 登陆数据库