Home > Mobile >  Is it possible to work with json type in java spring boot?
Is it possible to work with json type in java spring boot?

Time:11-10

Is it possible to work with json type as in javascript without generating JSONOject in java spring boot? Is it possible to work with the { } format without creating a JSONObject?

CodePudding user response:

JSON stands for "JavaScript Object Notation". The Java Syntax does not support JSON. Therefore, we cannot use JSON to declare any object in Java like

MyAwesomeObject object = { "name": "foo", "age": 42 };

We can, however, convert a String that contains a JSON-string into a JSONObject. See this question by dogbane for details.

If we use, for example, jackson, we can convert a String to an object through jackson's ObjectMapper:

final String json = "{ \"name\": \"foo\", \"age\": 42 }";
try {
    MyAwesomeObject object = objectMapper.readValue(json, MyAwesomeObject.class);
} catch (IOException e) {
    ...
}

For more details, I recommend reading this tutorial from baeldung.com.

  • Related