Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. {
  2. "foo": "bar",
  3. "baz": [ "qux","quxx"],
  4. "corge": null,
  5. "grault": 1,
  6. "garply": true,
  7. "waldo": "false",
  8. "fred": "undefined",
  9. "emptyArray": [],
  10. "emptyObject": {},
  11. "emptyString": ""
  12. }
  13.  
  14. foo: "bar"
  15. baz:
  16. - "qux"
  17. - "quxx"
  18. corge: null
  19. grault: 1
  20. garply: true
  21. waldo: "false"
  22. fred: "undefined"
  23. emptyArray: []
  24. emptyObject: {}
  25. emptyString: ""
  26.  
  27. import java.io.IOException;
  28.  
  29. import com.fasterxml.jackson.core.JsonProcessingException;
  30. import com.fasterxml.jackson.databind.JsonNode;
  31. import com.fasterxml.jackson.databind.ObjectMapper;
  32. import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
  33.  
  34. public class Library {
  35.  
  36. public String asYaml(String jsonString) throws JsonProcessingException, IOException {
  37. // parse JSON
  38. JsonNode jsonNodeTree = new ObjectMapper().readTree(jsonString);
  39. // save it as YAML
  40. String jsonAsYaml = new YAMLMapper().writeValueAsString(jsonNodeTree);
  41. return jsonAsYaml;
  42. }
  43.  
  44. }
  45.  
  46. compile 'com.fasterxml.jackson.core:jackson-core:2.8.6'
  47. compile 'com.fasterxml.jackson.core:jackson-databind:2.8.6'
  48. compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.8.6'
  49.  
  50. // this is your json object
  51. JSONObject jsonobject = new JSONObject(map);
  52. // get json string
  53. String prettyJSONString = jsonobject.toString(4);
  54. // mapping
  55. Map<String,Object> map = (Map<String, Object>) yaml.load(prettyJSONString);
  56. // convert to yaml string (yaml formatted string)
  57. String output = yaml.dump(map2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement