一、fastjson
引入包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.16</version>
</dependency>html
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)java
二、jacksonnode
引入的包
<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
JSON字符串裝javaBean
objectMapper = new ObjectMapper();
JavaBean javaBean= objectMapper.readValue(jsonString, JavaBean.class);數組
javaBean轉JSON字符串
objectMapper = new ObjectMapper();
//writeObject能夠轉換java對象,eg:JavaBean/Map/List/Array等app
String jsonString = objectMapper.writeValueAsString(bean);測試
控制檯打印:.net
jsonGenerator.writeObject(bean);
//writeValue具備和writeObject相同的功能
objectMapper.writeValue(System.out, bean);orm
獲取子節點
ObjectMapper mapper = new ObjectMapper();
// 讀取json,將整個json做爲根節點
JsonNode node = mapper.readTree(jsonString);xml
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
三、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