最近項目中要用json,閑暇時間,對json進行下總結。
1.JsonNode
項目中用到的jar包
-
import com.fasterxml.jackson.core.JsonParseException;
-
import com.fasterxml.jackson.databind.JsonMappingException;
-
import com.fasterxml.jackson.databind.JsonNode;
-
import com.fasterxml.jackson.databind.ObjectMapper;
-
import springfox.documentation.schema.Entry;
-
import java.io.IOException;
-
import java.util.*;
-
String json = "{\"username\":\"tom\",\"company\":{\"companyName\":\"中華\",\"address\":\"北京\"},\"cars\":[\"奔馳\",\"寶馬\"]"};
-
String arrayJson = "[{\"number\":64,\"result\":\"SUCCESS\"},{\"number\":65,\"result\":\"FAILURE\"},{\"number\":66,\"result\":\"ABORTED\"},{\"number\":67,\"result\":\"SUCCESS\"}]";
Json字符串轉換成JsonNode對象
-
ObjectMapper mapper = new ObjectMapper();
-
JsonNode jsonNode = mapper.readTree(json);
jsonNode的fieldNames方法是獲取jsonNode的所有的key值
-
Iterator<String> keys = jsonNode.fieldNames();
-
while(keys.hasNext()){
-
String key = keys.next();
-
System.out.println( "key鍵是:"+key);
-
}
根據key值獲取對應的值
-
JsonNode path = jsonNode.path( "username");
-
JsonNode resultValue = jsonNode.findValue( "username");
-
JsonNode resultPath = jsonNode.findPath( "username");
如果value為String,可以這樣讀取jsonNode的asText()方法獲取到字符串,其他類型可以jsonNode.findValue("number").asInt();這幾種方法都可以根據key獲取到相應的值,匹配到一個就停止搜索。resultValue和resultPath的區別在於,如果沒有匹配到任何key值為性別,resultValue為null,resultPath為空JsonNode,第一種的區別不是很清楚。
如果是一個JsonNode數組,使用jsonNode.elements();讀取數組中每個node, 如果不是JsonNode數組,使用jsonNode.elements();返回jsonNode的values
-
Iterator<JsonNode> elements = jsonNode.elements();
-
while(elements.hasNext()){
-
JsonNode node = elements.next();
-
System.out.println(node.toString());
-
}
取出所有key值為number的JsonNode的List
-
List<JsonNode> findKeys = jsonNode.findParents( "number");
-
for (JsonNode result:findKeys){
-
System.err.println(result.toString());
-
}
取出所有key值為number對應的value(如果value中包含子jsonNode並且子jsonNode的key值也為number,是無法捕獲到並加入list的)
-
List<JsonNode> findValues = jsonNode.findValues( "number");
-
for(JsonNode value: findValues){
-
System.out.println( value.toString());
-
}
遍歷某個JsonNode的key和value(value可能是字符串也可能是子jsonNode,但如果value是jsonNode數組的話,是無法讀取的)
-
Iterator<Map.Entry<String,JsonNode>> jsonNodes = jsonNode.fields();
-
while (jsonNodes.hasNext()) {
-
Map.Entry<String, JsonNode> node = jsonNodes.next();
-
System.err.println( "遍歷獲取key:"+node.getKey());
-
System.err.println( "遍歷獲取值:"+node.getValue().toString());
-
}
如果是JDK1.8的話,可以這樣遍歷JsonNode的子節點
-
jsonNode.forEach((JsonNode node)->{
-
System.out.println( "結果:"+node.toString());
-
});
JsonNode對象轉換成JSON字符串
-
String jsonStr = mapper.writeValueAsString(jsonNode);
-
System.out.println( "JsonNode--->Json:"+jsonStr);
2.JsonObject( fastjson)
json字符串轉換成JsonObject對象
-
String studentJson = "{\"stuId\":\"116\",\"stuName\":\"趙雲\",\"stuAddress\":\"常山\",\"stuIQ\":\"93\"}";
-
JSONObject object = JSON.parseObject(studentJson);
JsonObject對象轉換成JavaBean
Student student = object.toJavaObject(Student.class);
json字符串轉換成JavaBean
Student stu = JSON.parseObject(studentJson,Student.class);
String name = object.getString("stuName");
其他的常用方法
-
public static final Object parse(String text); // 把JSON文本parse為JSONObject或者JSONArray
-
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
-
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合
-
public static final String toJSONString(Object object); // 將JavaBean序列化為JSON文本
-
public static final String toJSONString(Object object, boolean prettyFormat); // 將JavaBean序列化為帶格式的JSON文本
-
public static final Object toJSON(Object javaObject); // 將JavaBean轉換為JSONObject或者JSONArray。