I am getting some data which is a single string
"{"somekey": someValue}, {"someKey2": someValue}, {"someKey3": someValue}"
how would I return that as a single json object like this using java libraries?
{{"somekey": someValue}, {"someKey2": someValue}, {"someKey3": someValue}}
I have been trying to use the ObjectMapper class to read the value into a List but can't convert it.
List<String> list = mapper.readValue(jsonString, new TypeReference<List<String>> () {});
I have the option to retrieve the data in an array like this:
[{"somekey": someValue}, {"someKey2": someValue}, {"someKey3": someValue}]
but I still can't manage to convert it to a single json Object response
CodePudding user response:
String jsonString = "{"somekey": someValue}, {"someKey2": someValue}, {"someKey3": someValue}";
Object object = new ObjectMapper().readValue(jsonString, Object.class);
Try this
CodePudding user response:
If you have a JSON string that looks as follows:
String userJson = "[{'name': 'Alex','id': 1}, "
"{'name': 'Brian','id':2}, "
"{'name': 'Charles','id': 3}]";
And a corresponding POJO that looks as follows:
public class User
{
private long id;
private String name;
@Override
public String toString() {
return "User [id=" id ", name=" name "]";
}
}
You could then use a library GSON to parse the JSON string into an array of User objects:
Gson gson = new Gson();
User[] userArray = gson.fromJson(userJson, User[].class);