What type of variable can I assign this to in order to bring the data into my environment?
{
"list": [{
"name": "first",
"key": ["1", "2", "3"]
}, {
"name": "second",
"key": ["4", "5", "6"]
}]
}
What type of variable can I assign this to in order to bring the data into my environment?
{
"list": [{
"name": "first",
"key": ["1", "2", "3"]
}, {
"name": "second",
"key": ["4", "5", "6"]
}]
}
First of all convert this Json to POJO class using some online JSONtoPOJO converter available .
Now use some ObjectMappers Libraries available such as GSON / Jackson1/Jackson2 .
you can easliy map your JSON to the POJO you created . for Example if you had Response POJO as Response.class you can Map the same like this
Gson gson = new Gson();
Response response = gson.fromJson(json.asString(),Response.class)
In case you JSON is not so Complex and don't have so many child nodes you can directly map the same to Map.class and get and Map of .
Map<Object,Object> map = gson.fromJson(json.asString(), Map.class);
Same is the case with jackson objectMapper . you can also try those.
One can not assign valid JSON to a Java variable or object type. But how do we import valid JSON into our classes if we really want to? To do this, JSON must be converted to a JSON String. There are many tools online which do this for us. Alternatively, valid JSON can be read from file by a Reader.
tldr: Actually getting valid JSON into Java class files is not obvious for those of us who are learning or doing testing.