java 将 JSON 转换为 YAML。将 JSON 解析为 YAML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26235350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:32:12  来源:igfitidea点击:

Convert JSON to YAML. Parsing JSON to YAML

javajsonparsingyaml

提问by eabyshev

I'm working with configuration files so I need to convert JSON to YAML. For example I have yaml file:

我正在处理配置文件,所以我需要将 JSON 转换为 YAML。例如我有 yaml 文件:

{
  "foo": "bar",
  "baz": [ "qux","quxx"],
  "corge": null,
  "grault": 1,
  "garply": true,
  "waldo": "false",
  "fred": "undefined",
  "emptyArray": [],
  "emptyObject": {},
  "emptyString": ""
}

The result should be yaml:

结果应该是 yaml:

foo: "bar"
baz: 
  - "qux"
  - "quxx"
corge: null
grault: 1
garply: true
waldo: "false"
fred: "undefined"
emptyArray: []
emptyObject: {}
emptyString: ""

Could you help me?

你可以帮帮我吗?

采纳答案by eabyshev

If you need convert JSONobject to yaml (string). you need. Firstly get json string, then map, after that you can convert to yaml. Here the code:

如果您需要将 JSONobject 转换为 yaml(字符串)。你需要。首先获取json字符串,然后映射,然后您可以转换为yaml。这里的代码:

  // this is your json object
  JSONObject jsonobject = new JSONObject(map);
  // get json string
  String prettyJSONString = jsonobject.toString(4);
  // mapping
  Map<String,Object> map = (Map<String, Object>) yaml.load(prettyJSONString);
  // convert to yaml string (yaml formatted string)
  String output = yaml.dump(map2);

回答by Tanya

You can convert JSON to YAML with two lines of code in Hymanson:

您可以使用Hymanson 中的两行代码将 JSON 转换为 YAML :

import java.io.IOException;

import com.fasterxml.Hymanson.core.JsonProcessingException;
import com.fasterxml.Hymanson.databind.JsonNode;
import com.fasterxml.Hymanson.databind.ObjectMapper;
import com.fasterxml.Hymanson.dataformat.yaml.YAMLMapper;

public class Library {

    public String asYaml(String jsonString) throws JsonProcessingException, IOException {
        // parse JSON
        JsonNode jsonNodeTree = new ObjectMapper().readTree(jsonString);
        // save it as YAML
        String jsonAsYaml = new YAMLMapper().writeValueAsString(jsonNodeTree);
        return jsonAsYaml;
    }

}

You will need to add dependencies to Hymanson Core, DataBind and DataFormat YAML. Below is a snippet for Gradle:

您需要向 Hymanson Core、DataBind 和 DataFormat YAML 添加依赖项。下面是 Gradle 的一个片段:

compile 'com.fasterxml.Hymanson.core:Hymanson-core:2.8.6'
compile 'com.fasterxml.Hymanson.core:Hymanson-databind:2.8.6'
compile 'com.fasterxml.Hymanson.dataformat:Hymanson-dataformat-yaml:2.8.6'

回答by Erik Aronesty

Here's a one liner for a file, suitable for sticking in a bash script. This should work on most default pythons on most systems:

这是一个文件的单行代码,适合粘贴在 bash 脚本中。这应该适用于大多数系统上的大多数默认 python:

python -c 'import json; import yaml; print(yaml.dump(json.load(open("inputfile"))))'

回答by Geo Systems

I'd suggest you consider Data Transformer(disclaimer - I'm its developer). It converts between JSON, YML, and other formats.

我建议您考虑使用Data Transformer(免责声明 - 我是它的开发人员)。它可以在 JSON、YML 和其他格式之间进行转换。

You can get it from the Mac App Storeor the Microsoft Store.

您可以从Mac App StoreMicrosoft Store获取它。

回答by bystander

http://jsontoyaml.com/

http://jsontoyaml.com/

this website may can help you. It can be used in Bash, JavaScript, JavaScript (browser only), Ruby, Python, Perl, Java..

这个网站可以帮到你。它可以在 Bash、JavaScript、JavaScript(仅限浏览器)、Ruby、Python、Perl、Java 中使用。

回答by Richard Gomes

function yaml_validate {
  python -c 'import sys, yaml, json; yaml.safe_load(sys.stdin.read())'
}

function yaml2json {
  python -c 'import sys, yaml, json; print(json.dumps(yaml.safe_load(sys.stdin.read())))'
}

function yaml2json_pretty {
  python -c 'import sys, yaml, json; print(json.dumps(yaml.safe_load(sys.stdin.read()), indent=2, sort_keys=False))'
}

function json_validate {
  python -c 'import sys, yaml, json; json.loads(sys.stdin.read())'
}

function json2yaml {
  python -c 'import sys, yaml, json; print(yaml.dump(json.loads(sys.stdin.read())))'
}

More Bash tricks at http://github.com/frgomes/bash-scripts

更多 Bash 技巧,请访问http://github.com/frgomes/bash-scripts