I think it should work. I've executed this code and it works fine. Here is my example.
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class TestJackson {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String testJson = "{\n" + " \"user\": {\n" + " \"0\": {\n" + " \"firstName\": \"Monica\",\n" + " \"lastName\": \"Belluci\"\n" + " },\n" + " \"1\": {\n" + " \"firstName\": \"John\",\n" + " \"lastName\": \"Smith\"\n" + " },\n" + " \"2\": {\n" + " \"firstName\": \"Owen\",\n" + " \"lastName\": \"Hargreaves\"\n" + " }\n" + " }\n" + "}";
User readValue = mapper.readValue(testJson, User.class);
System.out.println("readValue = " + readValue);
}
}
and the User.class:
import java.util.HashMap;
import java.util.Map;
class User {
private Map<String, MyObject> user = new HashMap<String, MyObject>();
public Map<String, MyObject> getUser() {
return user;
}
public void setUser(Map<String, MyObject> user) {
this.user = user;
}
@Override
public String toString() {
return "User{" +
"user=" + user +
'}';
}
}
class MyObject {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "MyObject{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
Answer from K. Gol on Stack Overflowjava - How to convert the following JSON String to POJO - Stack Overflow
How to convert json string to pojo if json response starts with [] instead of {} ??
How to convert effectively JSON to POJO using industry standard
How to generate POJOs from JSON?
I think it should work. I've executed this code and it works fine. Here is my example.
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class TestJackson {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String testJson = "{\n" + " \"user\": {\n" + " \"0\": {\n" + " \"firstName\": \"Monica\",\n" + " \"lastName\": \"Belluci\"\n" + " },\n" + " \"1\": {\n" + " \"firstName\": \"John\",\n" + " \"lastName\": \"Smith\"\n" + " },\n" + " \"2\": {\n" + " \"firstName\": \"Owen\",\n" + " \"lastName\": \"Hargreaves\"\n" + " }\n" + " }\n" + "}";
User readValue = mapper.readValue(testJson, User.class);
System.out.println("readValue = " + readValue);
}
}
and the User.class:
import java.util.HashMap;
import java.util.Map;
class User {
private Map<String, MyObject> user = new HashMap<String, MyObject>();
public Map<String, MyObject> getUser() {
return user;
}
public void setUser(Map<String, MyObject> user) {
this.user = user;
}
@Override
public String toString() {
return "User{" +
"user=" + user +
'}';
}
}
class MyObject {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "MyObject{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
Use can done with the help of gson library.
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class JsonToJava {
public static void main(String[] args) throws IOException {
try(Reader reader = new InputStreamReader(JsonToJava.class.getResourceAsStream("/Server2.json"), "UTF-8")){
Gson gson = new GsonBuilder().create();
Person p = gson.fromJson(reader, YourPOJOClass.class);
System.out.println(p);
}
}
}
visit this link hope this helps :)
Hello all, I have a json sample format and a pojo class and I want to check if my json values matches with pojoโs corresponding json value I am using objectmapper to do this using a code written as below but its giving this exception/, Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `com.ingram.api.clientIngram.dto.RenewalsSearchRequest` from Array value (token `JsonToken.START_ARRAY`), I am trying to understand this and tyring everything I could do acc. to my understanding but not getting anywhere I have a class for this response as RequestBody and it has,The code that I am trying to run is :
private List<RenewalPayload> renewalPayload;
public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
ObjectMapper ob = new ObjectMapper();
String jsonValue = "[\n"+ " {\n"+ " \"status\": {\n"+ " \"OpporutinyStatus\": {\n"+ " \"value\": \"Closed\",\n"+ " \"subStatus\": \"Renewal went direct\"\n"+ " }\n"+ " },\n"+ " \"vendor\": \"HP\",\n"+ " \"endUser\": \"STARK\"\n"+ " }\n"+ "]";
RenewalsSearchRequest sor = ob.readValue(jsonValue, RenewalsSearchRequest.class);
System.out.println("converted....." + sor.toString());
String json = ob.writeValueAsString(sor);
System.out.println("obj to json...." + json);
The json format that I need to match with is ,
[
{
"status": {
"OpporutinyStatus": {
"value": "Closed",
"subStatus": "Renewal went direct"
}},
"vendor": "HP",
"endUser": "STARK"
}
]
and payload class has these fields,
private Status status;
private String vendor;
private String endUser;
and status class consists :
private OpporutinyStatus opporutinyStatus;
now what to do ?? please help ? and opporutinyStatus has fields :
@Datapublic class OpporutinyStatus { private String value;
private String subStatus;
}
I have this API which https://api.nytimes.com/svc/topstories/v2/arts.json?api-key=xyz
which gives a complex json structure result. I need title,section from these to map to my pojo containing same feilds .
I used Map structure matching json structure and got feilds but i dont feel its the right way, any industry standard way?pls help.
uri in spring boot:
Map<String,ArrayList<Map<String,String>>> res = new HashMap<String, ArrayList<Map<String,String>>>();
ResponseEntity<Map> s= restTemplate.getForEntity(
"https://api.nytimes.com/svc/topstories/v2/arts.json?api-key=xyz",
Map.class);
res =s.getBody();
after this i get values from Map inside arraylist.
sample JSON data is in comments
java class:
@JsonIgnoreProperties(ignoreUnknown = true)
public class News {
//private Results[] results;
private String title;
private String section;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
private String url;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public News(String title, String section, String url) {
this.title = title;
this.section = section;
this.url = url;
}
public News() {
super();
}
}