Json到yaml转换不起作用?

时间:2015-06-05 09:48:49

标签: json jackson yaml

我正在尝试将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);
        }
    }
}

输出(错误的yaml): -

    --- |
[ {
  "name" : "Murugesan"
}, {
  "age" : "20"
} ]

预期输出: -

--- !
  name: Murugesan    
  age: 20

1 个答案:

答案 0 :(得分:2)

使用下面的代码

将json对象转换为map
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;
    }

}

使用此演示cilent

 public class JSONTOyaml {

    public void jsonToYaml(JSONObject json) throws JSONException, org.codehaus.jettison.json.JSONException, FileNotFoundException{
        System.out.println(Yaml.dump(JsonToMap.jsonToMap(json)));
        Yaml.dump(Yaml.dump(JsonToMap.jsonToMap(json)), new File("config.yml"));
    }
    public static void main(String args[]) throws JSONException, IOException, YamlException, org.codehaus.jettison.json.JSONException {
        JSONTOyaml out=new JSONTOyaml();
        String json1="{'asia': {'country': {'KR': '10.11.12.3,1.1.1.1', 'JP': '1,2,3', 'IN': 'DenmarkChennal'}, "
                + "'www.wwe.com': {'vasanth': 'ch1,ch2,ch3', 'default': 'ch2,ch3'},"
                + " 'isp': {'JAPN-COM': '1,3,4,5,6,6', 'Aircel': 'AirtelChennal', 'AIRTEL': '2,3,3,3,3,3,3,3,3'}, 'studioDefault': 'studioDefault-ThaivanStudio'}}";
        JSONObject json=new JSONObject(json1);
        out.jsonToYaml(json);
        }
    }

试试这段代码。它会起作用