I have a JSON file:
{
"param": {
"rows": 1,
"columns": "4"
},
"items": [{
"name": "A",
"amount": 33,
"price": "43"
}, {
"name": "B",
"amount": 43,
"price": "2"
}, {
"name": "C",
"amount": 45,
"price": "1"
}, {
"name": "D",
"amount": 543,
"price": "55"
}]
}
I want to get the items separately. I try to do, but the result did null:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Items item; // only items
item = objectMapper.readValue(new File("D:/example/file.json"), Items.class);
System.out.println(item.getAmount());
This is class for Items:
public class Items {
private String name;
private String amount;
private double price;
public Items() {
super();
}
//constructor
//getters and setters
}
What I do wrong? how correctly read value from items?
CodePudding user response:
You didn't handle the JSON array - items - properly, so as I commented under OP, all what you need to do is to rename original class Items to Item and create another class Items as follows, and the Jackson library will do the rest for you:
public class Item {
private String name;
private String amount;
private double price;
// general getters/setters
}
public class Items {
private List<Item> items;
// general getter/setter
}
Then you can deserialize the JSON string with your code snippet:
Items items = objectMapper.readValue(new File("D:/example/file.json"), Items.class);
System.out.println(items.get(0).getAmount());
CodePudding user response:
firstly, you need to have an object, that contain List item, because the JSON file have multiple items, not only one, you can refer my code
public class ItemsList {
private List<Items> items;
public ItemsList () {
}
}
and we will modify the mapper
item= objectMapper.readValue(new File("D:/example/file.json"), ItemsList.class);
but i think you should change your class from Items => Item
hope it help you solve problem
CodePudding user response:
I suggest you to easily have the following classes, Example.java, Param.java, Item.java:
Example.java:
public class Example {
private Param param;
private List<Item> items;
//constructor
//getters and setters
}
Param.java:
public class Param {
private int rows;
private int columns;
//constructor
//getters and setters
}
Item.java
public class Item {
private String name;
private String amount;
private double price;
}
Then you can write:
Example example= objectMapper.readValue(new File("D:/example/file.json"),
Example.class);
for(Item item: example.getItems())
System.out.println(item.getAmount());
CodePudding user response:
Problem here is because structure of your JSON file is not correspond to the structure of your class that you pass as a second parameter in ObjectMapper.readValue method (Items.class in your example). You need to align both structures.
According to the file structure, your Items file should looks next
public class Items {
private Param param;
private List<Item> items;
// Getters & Setters
}
Additionaly you need to create class Param:
public class Param {
private Integer rows;
private Integer columns;
// Getters & Setters
}
And class Item:
public class Item {
private String name;
private String amount;
private double price;
// Getters & Setters
}
After that you will be able to read your file by next snippet:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Items items = objectMapper.readValue(new File("D:/example/file.json"), Items.class);
items.getItems().stream().map(Item::getAmount).forEach(System.out::println);
CodePudding user response:
You should read it as Map<String, Object> and then use your map to as a source fill your items. Also, there is an open-source library that has a feature based on Jackson library really simplifies reading/writing objects from/to Json. If you have a json string all you have to do is:
Map<String, Object> map = JsonUtils.readObjectFromJsonString(jsonString, Map.class);
It throws IOException though so you will need to handle it. Here is the Javadoc. The library is called MgntUtils and could be found as Maven artifact and on the Github (including javadoc and source code)
