如何在Java中将YAML转换为JSON? [英] How do I convert from YAML to JSON in Java?

查看:1343
本文介绍了如何在Java中将YAML转换为JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想将包含yaml的字符串转换为包含使用Java的corrseponding转换json的另一个字符串。

I just want to convert a string that contains a yaml into another string that contains the corrseponding converted json using Java.

例如,我有内容这个yaml

For example supose that I have the content of this yaml

---
paper:
   uuid: 8a8cbf60-e067-11e3-8b68-0800200c9a66
   name: On formally undecidable propositions of Principia Mathematica and related systems I.
   author: Kurt Gödel.
tags:
   - tag:
       uuid: 98fb0d90-e067-11e3-8b68-0800200c9a66
       name: Mathematics
   - tag:
       uuid: 3f25f680-e068-11e3-8b68-0800200c9a66
       name: Logic

名为yamlDoc的字符串中的

in a String called yamlDoc:

String yamlDoc = "---\npaper:\n   uuid: 8a... etc...";

我想要一些方法可以将yaml String转换为另一个带有相应json的String,即以下代码

I want some method that can convert the yaml String into another String with the corresponding json, i.e. the following code

String yamlDoc = "---\npaper:\n   uuid: 8a... etc...";
String json = convertToJson(yamlDoc); // I want this method
System.out.println(json);

应打印:

{
    "paper": {
        "uuid": "8a8cbf60-e067-11e3-8b68-0800200c9a66",
        "name": "On formally undecidable propositions of Principia Mathematica and related systems I.",
        "author": "Kurt Gödel."
    },
    "tags": [
        {
            "tag": {
                "uuid": "98fb0d90-e067-11e3-8b68-0800200c9a66",
                "name": "Mathematics"
            }
        },
        {
            "tag": {
                "uuid": "3f25f680-e068-11e3-8b68-0800200c9a66",
                "name": "Logic"
            }
        }
    ]
}

我想知道在这个例子中是否存在与 convertToJson()方法类似的东西。

I want to know if exists something similar to the convertToJson() method in this example.

我尝试使用 SnakeYAML 来实现这一目标,所以这个代码

I tried to achieve this using SnakeYAML, so this code

 Yaml yaml = new Yaml();
 Map<String,Object> map = (Map<String, Object>) yaml.load(yamlDoc);

构造一个包含已解析的YAML结构的地图(使用嵌套的地图)。然后,如果有一个可以将地图转换为json字符串的解析器,它将解决我的问题,但我也没有找到类似的东西。

constructs a map that contain the parsed YAML structure (using nested Maps). Then if there is a parser that can convert a map into a json String it will solve my problem, but I didn't find something like that neither.

任何回复都将非常感谢。

Any response will be greatly appreciated.

推荐答案

感谢HotLicks提示(在问题评论中)我终于使用库 org.json SnakeYAML 以这种方式:

Thanks to HotLicks tip (in the question comments) I finally achieve the conversion using the libraries org.json and SnakeYAML in this way:

private static String convertToJson(String yamlString) {
    Yaml yaml= new Yaml();
    Map<String,Object> map= (Map<String, Object>) yaml.load(yamlString);

    JSONObject jsonObject=new JSONObject(map);
    return jsonObject.toString();
}

我不知道这是否是最佳方式,但它适合我。

I don't know if it's the best way to do it, but it works for me.

这篇关于如何在Java中将YAML转换为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆