Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "foo": "bar",
- "baz": [ "qux","quxx"],
- "corge": null,
- "grault": 1,
- "garply": true,
- "waldo": "false",
- "fred": "undefined",
- "emptyArray": [],
- "emptyObject": {},
- "emptyString": ""
- }
- foo: "bar"
- baz:
- - "qux"
- - "quxx"
- corge: null
- grault: 1
- garply: true
- waldo: "false"
- fred: "undefined"
- emptyArray: []
- emptyObject: {}
- emptyString: ""
- import java.io.IOException;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.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;
- }
- }
- compile 'com.fasterxml.jackson.core:jackson-core:2.8.6'
- compile 'com.fasterxml.jackson.core:jackson-databind:2.8.6'
- compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.8.6'
- // 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);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement