You can go through this tutorial. Hope it will help you.
- How to convert Java object to / from JSON (Jackson)
- https://dzone.com/articles/deserializing-json-java-object
You can go through this tutorial. Hope it will help you.
- How to convert Java object to / from JSON (Jackson)
- https://dzone.com/articles/deserializing-json-java-object
That's json. You need to parse it using api.
For example
{'profiles': [{'name':'john', 'age': 44}, {'name':'Alex','age':11}]}
you will have to do something of this effect:
JSONObject myjson = new JSONObject(the_json);
JSONArray the_json_array = myjson.getJSONArray("profiles");
this returns the array object.
Then iterating will be as follows:
int size = the_json_array.length();
ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();
for (int i = 0; i < size; i++) {
JSONObject another_json_object = the_json_array.getJSONObject(i);
//Blah blah blah...
arrays.add(another_json_object);
}
//Finally
JSONObject[] jsons = new JSONObject[arrays.size()];
arrays.toArray(jsons);
Example code is taken from How to parse a JSON and turn its values into an Array?
Deserialize JSON file into Java object - Stack Overflow
Deserialization of JSON from an API
Having trouble with deserialization of JSON String to a Java object using Gson
Why can't I deserialize JSON that I had serialized?
You can use ObjectMapper for this:
ObjectMapper objectMapper = new ObjectMapper();
String jsonRecordArray =
"[{ \"name\" : \"David\", \"age\" : 45 },\n" +
" { \"name\" : \"Norma\", \"age\" : 44 }\n" +
"]";
List<Record> listRecord = objectMapper.readValue(jsonRecordArray, new TypeReference<>() {
});
In maven the relevant dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.2.2</version>
</dependency>
Remember to handle JsonProcessingException, for example by adding it to the method signature.
First of all the answer from jwpol is correct and could be used as a solution. Just a bit of additional clarifications: The library that @jwpol suggests is called JSON-Jackson or also known as faster XML. Here is the main site for it and there are many others. This library is de-facto is JSON standard (used by default in Spring Boot). One popular alternative would be Gson - a Google implementation of JSON library. Here are 2 sites to read about it: https://github.com/google/gson, https://sites.google.com/site/gson/gson-user-guide.
Also, for simplistic use where you just need simple serialization/de-serialization capabilities I wrote my own simple feature that is a thin wrapper over Jackson Json library that gives you simplified use without directly importing Jackson library and configuring it. With my feature your code could look like
String jsonRecordArray =
"[{ \"name\" : \"David\", \"age\" : 45 },\n" +
" { \"name\" : \"Norma\", \"age\" : 44 }\n" +
"]";
List<Record> listRecord;
try {
listRecord = JsonUtils.readObjectFromJsonString(jsonRecordArray, List.class);
} catch(IOException ioe) {
...
}
BTW, in jwpol exaple exception handling is missing.
If you want to use my feature you need to import MgntUtils open source library written by me. You can get Maven artifacts here and or get it on Github (including source code and Javadoc). Here is Javadoc for JsonUtils class