Json到yaml的转换不起作用?

Json到yaml的转换不起作用?,json,jackson,yaml,Json,Jackson,Yaml,我正在尝试将json对象转换为yaml文件。但我得到的yaml文件不正确。有人帮我解决这个问题 Java代码:- public class Trials { private JsonNodeFactory nodeFactory; public ArrayNode getJSONObject() { nodeFactory = JsonNodeFactory.instance; ArrayNode obj1 = nodeFactory.arr

我正在尝试将json对象转换为yaml文件。但我得到的yaml文件不正确。有人帮我解决这个问题

Java代码:-

 public class Trials {

    private JsonNodeFactory nodeFactory;

    public ArrayNode getJSONObject() {
        nodeFactory = JsonNodeFactory.instance;
        ArrayNode obj1 = nodeFactory.arrayNode();
        ObjectNode obj11 = nodeFactory.objectNode();
        ObjectNode obj12 = nodeFactory.objectNode();
        obj11.put("name", "Murugesan");
        obj12.put("age", "20");
        obj1.insert(1, obj11);
        obj1.insert(2, obj12);
        return obj1;
    }

    public static void main(String args[]) throws JsonGenerationException,
            JsonMappingException, IOException {
        Trials trial = new Trials();
        ObjectMapper mapper = new ObjectMapper();
        String data = mapper.defaultPrettyPrintingWriter().writeValueAsString(
                trial.getJSONObject());
        Yaml.dump(data, new File("object.yml"));

        BufferedReader br = new BufferedReader(new FileReader("object.yml"));

        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }
}
    --- |
[ {
  "name" : "Murugesan"
}, {
  "age" : "20"
} ]
--- !
  name: Murugesan    
  age: 20
输出(不正确的yaml):-

 public class Trials {

    private JsonNodeFactory nodeFactory;

    public ArrayNode getJSONObject() {
        nodeFactory = JsonNodeFactory.instance;
        ArrayNode obj1 = nodeFactory.arrayNode();
        ObjectNode obj11 = nodeFactory.objectNode();
        ObjectNode obj12 = nodeFactory.objectNode();
        obj11.put("name", "Murugesan");
        obj12.put("age", "20");
        obj1.insert(1, obj11);
        obj1.insert(2, obj12);
        return obj1;
    }

    public static void main(String args[]) throws JsonGenerationException,
            JsonMappingException, IOException {
        Trials trial = new Trials();
        ObjectMapper mapper = new ObjectMapper();
        String data = mapper.defaultPrettyPrintingWriter().writeValueAsString(
                trial.getJSONObject());
        Yaml.dump(data, new File("object.yml"));

        BufferedReader br = new BufferedReader(new FileReader("object.yml"));

        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }
}
    --- |
[ {
  "name" : "Murugesan"
}, {
  "age" : "20"
} ]
--- !
  name: Murugesan    
  age: 20
预期输出:-

 public class Trials {

    private JsonNodeFactory nodeFactory;

    public ArrayNode getJSONObject() {
        nodeFactory = JsonNodeFactory.instance;
        ArrayNode obj1 = nodeFactory.arrayNode();
        ObjectNode obj11 = nodeFactory.objectNode();
        ObjectNode obj12 = nodeFactory.objectNode();
        obj11.put("name", "Murugesan");
        obj12.put("age", "20");
        obj1.insert(1, obj11);
        obj1.insert(2, obj12);
        return obj1;
    }

    public static void main(String args[]) throws JsonGenerationException,
            JsonMappingException, IOException {
        Trials trial = new Trials();
        ObjectMapper mapper = new ObjectMapper();
        String data = mapper.defaultPrettyPrintingWriter().writeValueAsString(
                trial.getJSONObject());
        Yaml.dump(data, new File("object.yml"));

        BufferedReader br = new BufferedReader(new FileReader("object.yml"));

        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }
}
    --- |
[ {
  "name" : "Murugesan"
}, {
  "age" : "20"
} ]
--- !
  name: Murugesan    
  age: 20

使用下面的代码将json对象转换为映射

public class JsonToMap {
    public static Map<String, Object> jsonToMap(JSONObject json) throws JSONException, org.codehaus.jettison.json.JSONException {
        Map<String, Object> retMap = new HashMap<String, Object>();

        if(json != JSONObject.NULL) {
            retMap = toMap(json);
        }
        return retMap;
    }

    public static Map<String, Object> toMap(JSONObject object) throws JSONException, org.codehaus.jettison.json.JSONException {
        Map<String, Object> map = new HashMap<String, Object>();

        Iterator<String> keysItr = object.keys();
        while(keysItr.hasNext()) {
            String key = keysItr.next();
            Object value = object.get(key);

            if(value instanceof JSONArray) {
                value = toList((JSONArray) value);
            }

            else if(value instanceof JSONObject) {
                value = toMap((JSONObject) value);
            }
            map.put(key, value);
        }
        return map;
    }

    public static List<Object> toList(JSONArray array) throws JSONException, org.codehaus.jettison.json.JSONException {
        List<Object> list = new ArrayList<Object>();
        for(int i = 0; i < array.length(); i++) {
            Object value = array.get(i);
            if(value instanceof JSONArray) {
                value = toList((JSONArray) value);
            }
            else if(value instanceof JSONObject) {
                value = toMap((JSONObject) value);
            }
            list.add(value);
        }
        return list;
    }

}
试试这个代码。这将是工作