I noticed that I lacked knowledge when dealing with JSON. I would like to summarize to the level that it is possible to handle JSON at least with java if there is only this (The library uses JSONIC)
When dealing with JSON in Java, handle String written in JSON format String→POJO POJO → String, etc.
① The list structure is enclosed in [] ② The POJO structure is surrounded by {} ③ There is no part that specifies the name of the POJO structure that is the parent I think you can read it as soon as you remember the above three
sample
{
"id": 1,
"name": "shoes",
"price": 12000,
"shops": [
{
"name": "Astore",
"location": "tokyo"
},
{
"name": "Bstore",
"location": "oosaka"
}
]
}
Sample POJO
public class Product {
public Integer id;
public String name;
public Integer price;
public List<Shop> shops;
}
public class Shop {
public String name;
public String location;
}
When using the above JSON and POJO
JSON→Java
String json = "{\"id\": 1,\"name\": \"shoes\",\"price\": 12000,\"shops\": [{\"name\": \"Astore\",\"location\": \"tokyo\"},{\"name\": \"Bstore\",\"location\": \"oosaka\"}]}";
ObjectMapper mapper = new ObjectMapper();
Product data = mapper.readValue(json, Product.class);
When sent in the structure of a list or map
ObjectMapper mapper = new ObjectMapper();
List<Product> products = mapper.readValue(json, new TypeReference<List<Product>>() {});
Java→JSON
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(product);
・ POJO will give an error if each property is pullic or does not have Getter / Setter. -An error will occur if there is a property that POJO does not have when converting from JSON to Java. → If you add "@JsonIgnoreProperties (ignoreUnknown = true)" to POJO, no error will occur. → Only necessary data can be extracted