【问题标题】:Json to yaml conversion does not work?Json 到 yaml 的转换不起作用?
【发布时间】:2015-08-20 05:18:21
【问题描述】:

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

【问题讨论】:

    标签: json jackson yaml


    【解决方案1】:

    使用以下代码将 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;
        }
    
    }
    

    使用这个演示程序

     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);
            }
        }
    

    试试这个代码。会成功的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      相关资源
      最近更新 更多