-2

I did not write a lot in Java yet, but I wonder if there is a simple way, to parse JSON and work with the results into a map just like we would do it in other modern languages:

string = loadFromSomeWhere(URI)
dictionary = JSON.parse(string)
// do something with the dictionary

I do not want to define a POJO first, nor will I deal with the deepness of the JSON structure. And I can assume, that the file will stay small enought, so event driven parsing is not necessary.

I need to use Jackson and found only way to complicated approaches.

Chris Pillen
  • 721
  • 7
  • 21
  • Where you're receiving your JSON? in Spring controller? or you have it as a literal String value? – Giorgi Tsiklauri Feb 08 '20 at 15:01
  • Why do you need to use Jackson? Java has had JSON parsing built in for _quite_ a while now - https://www.oracle.com/technical-resources/articles/java/json.html – Mike 'Pomax' Kamermans Feb 08 '20 at 15:02
  • Does this answer your question? [creating Hashmap from a JSON String](https://stackoverflow.com/questions/22011200/creating-hashmap-from-a-json-string) – papaya Feb 08 '20 at 15:20
  • There's a lot of material on this stuff online! Google is your friend: https://mkyong.com/java/how-to-convert-java-map-to-from-json-jackson/ https://stackoverflow.com/questions/2525042/how-to-convert-a-json-string-to-a-mapstring-string-with-jackson-json https://stackoverflow.com/questions/443499/convert-json-to-map https://www.baeldung.com/jackson-map http://websystique.com/java/json/jackson-convert-java-map-to-from-json/ https://howtodoinjava.com/jackson/jackson-json-to-from-hashmap/ – Roberto Manfreda Feb 08 '20 at 16:12
  • Moreover, if you want a typed Map (exploiting java generics), you can do : `Map typedMap = mapper.readValue(jsonStream, new TypeReference>() {}); ` -> from this link https://stackoverflow.com/questions/443499/convert-json-to-map – Roberto Manfreda Feb 08 '20 at 16:20
  • As far as the docs go, it is that JSON-P what you mean. But I still have to include a dependecy, don't I? @Mike'Pomax'Kamermans – Chris Pillen Feb 08 '20 at 17:22
  • It's the built in JSON parser, searching for any of the classes used in that article will tell you exactly which (built-in) package to import for your code to work. Or, if you're in Eclipse or another intelligent IDE, you should be able to just write the code and it'll go "hey you need to import xyz for this to work. Do that now?" and then you accept that suggestion. – Mike 'Pomax' Kamermans Feb 08 '20 at 23:03

1 Answers1

-1

You can use external Json libraries for java - such as com.google.gson - this would almost allow similar operation as JSON we use for javascript. you can initialize it as Gson gson = new Gson();

YouJaravObject obj = gson.fromJson(yourJsonString, YouJaravObject.class);

You can also have a map or a list - which your json string can be converted to.

if you use mvn build you need to also include gson into dependency list of build as below

<dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>

You can also use "jackson.databind" for more generic conversion

JsonNode jsonBody
ObjectMapper om = new ObjectMapper()
om.treeToValue(jsonBody.get("metaData"), abc.class);

which ll let you access your node / tree from json

Pravin Bansal
  • 2,543
  • 20
  • 12