java中使用fastjson、jackson、json-lib解析JSON-------------------妈妈再也不用担心JSON解析

1、fastjson
引入包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.16</version>
</dependency>

JSON字符串转JSONObject/JSONArray
JSONObject jsonObject = JSONObject.parseObject(jsonString);
得到子节点
jsonObject .getString();
jsonObject .getArray();
jsonObject .getDate();
.....
转为javaBean
JSONObject.toJavaObject(jsonObject,JavaBean.class);
JSON字符串直接转JavaBean
JSONObject.parseObject(jsonString,javaBean.class);
JavaBean转JSON字符串
String jsonString = JSONObject.toJSONString(javaBean)

2、jackson

引入的包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.0</version>
</dependency>

JSON字符串装javaBean
objectMapper = new ObjectMapper();
JavaBean javaBean= objectMapper.readValue(jsonString, JavaBean.class);

javaBean转JSON字符串
objectMapper = new ObjectMapper();
//writeObject可以转换java对象,eg:JavaBean/Map/List/Array等

String jsonString = objectMapper.writeValueAsString(bean);

控制台打印:

jsonGenerator.writeObject(bean);
//writeValue具有和writeObject相同的功能
objectMapper.writeValue(System.out, bean);

 

获取子节点
ObjectMapper mapper = new ObjectMapper();
// 读取json,将整个json作为根节点
JsonNode node = mapper.readTree(jsonString);

node.path();
node.get();

 

1. //如果属性没有值,那么Json是会处理的,int类型为0,String类型为null,数组为[],设置这个特性可以忽略空值属性
2. mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
3. //让Json可以缩进,可读性更好,一般用在测试和开发阶段。
4. mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
5. //让map的key按自然顺序排列
6. mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
7. //日期输出格式
8. SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
9. mapper.setDateFormat(outputFormat);

创建节点,生成JSON字符串
1. //创建一个提供所有节点的JsonNodeFactory,false表示不创建DecimalNode
2. JsonNodeFactory factory = new JsonNodeFactory(false);
3.
4. // 创建JsonFactory,使用Streaming API的方式输出到控制台
5. JsonFactory jsonFactory = new JsonFactory();
6. JsonGenerator generator = jsonFactory.createGenerator(System.out);
7. ObjectMapper mapper = new ObjectMapper();
8.
9. //创建节点和数据,一个ObjectNode代表一个节点对象
10. ObjectNode node1 = factory.objectNode();
11. ObjectNode node2 = factory.objectNode();
12. node1.put("A", "a");
13. node1.put("B", "b");
14. node2.set("C", node1);
15.
16. // 根节点
17. ObjectNode root = factory.objectNode();
18. root.put("root", "root");
19. root.set("children", node2);
20. mapper.writeTree(generator, root);
21. //输出{"root":"root","children":{"C":{"A":"a","B":"b"}}}

其他数组、list、map参考
http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html
http://blog.csdn.net/gjb724332682/article/details/51586701

 

3、json-lib
使用的包
<!--json-lib-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>

JSON字符串转JSONObject/JSONArray
JSONObject jsonObject = JSONObject.fromObject(jsonString/javaBean);
得到子节点
jsonObject.getString("success")
转为javaBean
JSONObject.toBean(jsonObject,JavaBean.class);
javaBean转JSON字符串
String jsonString=jsonObject.toString();

list map参照
http://www.cnblogs.com/teach/p/5791029.html

posted @ 2016-10-26 22:35  风吹过的绿洲  阅读(7777)  评论(1编辑  收藏  举报