一、maven jar包使用。 (项目没有使用maven的直接引用jar包,附件已上传)
pom.xml配置:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.17</version>
</dependency>
二、java文件 使用
1、引入jar包
import com.alibaba.fastjson.JSON;
2、使用
这种方式:
System.out.println(JSON.toJSONString(list));
com.alibaba.fastjson.JSONArray json_subject= com.alibaba.fastjson.JSONArray.parseArray(JSON.toJSONString(list));
System.err.println(json_subject.size());
或者是这种方式:
JSONObject obj=JSONObject.fromObject(JSON.toJSONString(list));
3、注意:
一定要用JSON.toJSONString(list)
这种情况的格式是:[{KANQCONTENT=<p>1111</p>, DETAILS=1, SOURCE=1, TITLE=1, TIME=2017-11-09, ID=3815c803-2cf1-472f-9e8f-068c4941b48f}]
若不进行JSON.toJSONString,对这列list内容进行JSONObject obj=JSONObject.fromObject(json_subject.get(i));转换时会出现
net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
上网搜索解决方案大部分是需要注入一个时间转换器,JsonConfig使用,可是我这怎么就是不走jsonconfig的方法。参考比较贴合实际信息http://piranha.iteye.com/blog/1973852。无奈找老同事,用这种方式搞定。简单管事。
若是这种情况的则不需要了。 [{"agreementNumber":"010","id":"9987962c-2dcf-4304-8420-bb7dd35762d0","operatorComCode":"yili"1}] ,而且这种格式的也不会出现问题。