Example

https://fasterxml.github.io/jackson-databind/javadoc/2.2.0/com/fasterxml/jackson/databind/JsonNode.html


-1. Jason String 转换成 JasonNode 对象

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
private final static ObjectMapper objectMapper = new ObjectMapper();
/**
 * Convert the Jason String to JsonNode object
 * 
 * @param output
 * @return
 */
public JsonNode getJsonNode(String input) {
    JsonNode node = null;
    try {
        node = objectMapper.readTree(input);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return node;
}

/**
 * JasonNode 常用方法
 */
JsonNode jsonNode = getJsonNode(input);

//getKeys
Iterator<String> keys = jsonNode.fieldNames();  
    while(keys.hasNext()){  
    String key = keys.next();  
    System.out.println("key="+key);

//get value by key name 
jsonNode.findValue("Key_number").asInt();
jsonNode.findValue("Key_string").asText();

jsonNode.forEach((JsonNode node)->{
  System.out.println("result=:"+node.toString());
});

String jsonStr = mapper.writeValueAsString(jsonNode);  
System.out.println("JsonNode--->Json:"+jsonStr);


-2. 读取Json文件,转换成Jason String

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
/**
 * Read json file to string
 * @param filepath
 * @return
 */
public String readJsonFile(String filepath) {
    // JSON parser object to parse read file
    JSONParser parser = new JSONParser();
    String jsonString = null;
    try {
        JSONObject jsonData = (JSONObject) parser.parse(new FileReader(filepath));
        jsonString = jsonData.toJSONString();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return jsonString;
}