英文:
Read JSON as a list of Object - Jackson
问题
I have a Json string like below
{
"EngineData": [
{
"make": "Toyota",
"transmission": "Manual",
"fuel": "Petrol"
},
{
"make": "GMC",
"transmission": "Auto",
"fuel": "Petrol"
},
{
"make": "Honda",
"transmission": "Manual",
"fuel": "Petrol"
}
]
}
I have the POJO defined like below
@JsonIgnoreProperties(ignoreUnknown = true)
public class EngineData implements Serializable {
@JsonProperty("make")
private String make;
@JsonProperty("transmission")
private String transmission;
@JsonProperty("fuel")
private String fuel;
//getters and setters
}
I wanted to read it to `List<EngineData>`
I wrote the following but it doesn't read the String to a EngineData list
List
ObjectMapper mapper = new ObjectMapper();
engineDataList = mapper.readValue(myJsonString, new TypeReference<List
});
Not sure what's wrong in this
<details>
<summary>英文:</summary>
I have a Json string like below
{
"EngineData": [
{
"make": "Toyota",
"transmission": "Manual",
"fuel": "Petrol"
},
{
"make": "GMC",
"transmission": "Auto",
"fuel": "Petrol"
},
{
"make": "Honda",
"transmission": "Manual",
"fuel": "Petrol"
}
]
}
I have the POJO defined like below
@JsonIgnoreProperties(ignoreUnknown = true)
public class EngineData implements Serializable {
@JsonProperty("make")
private String make;
@JsonProperty("transmission")
private String transmission;
@JsonProperty("fuel")
private String fuel;
//getters and setters
}
I wanted to read it to `List<EngineData>`
I wrote the following but it doesn't read the String to a EngineData list
List<EngineData> engineDataList =null;
ObjectMapper mapper = new ObjectMapper();
engineDataList = mapper.readValue(myJsonString, new TypeReference<List<EngineData>>() {
});
Not sure whats wrong in this
</details>
# 答案1
**得分**: 2
你的 JSON 对应于一个对象,如下所示:
```java
class CustomObject {
List<EngineData> EngineData;
//...
}
它将与以下 JSON 一起工作:
[
{
"make": "Toyota",
"transmission": "Manual",
"fuel": "Petrol"
},
{
"make": "GMC",
"transmission": "Auto",
"fuel": "Petrol"
},
{
"make": "Honda",
"transmission": "Manual",
"fuel": "Petrol"
}
]
如果你被迫使用这个 JSON,我认为你需要编写一个自定义的反序列化器。详见:https://www.baeldung.com/jackson-deserialization
英文:
Your JSON corresponds to an object such as :
class CustomObject {
List<EngineData> EngineData;
//...
}
It will work with bellow JSON :
[
{
"make": "Toyota",
"transmission": "Manual",
"fuel": "Petrol"
},
{
"make": "GMC",
"transmission": "Auto",
"fuel": "Petrol"
},
{
"make": "Honda",
"transmission": "Manual",
"fuel": "Petrol"
}
]
If you're forced to use this JSON I think you will need to write a custom deserializer. cf. https://www.baeldung.com/jackson-deserialization
答案2
得分: 0
对象无法映射到列表,
请参考以下内容以映射给定的 JSON:
public class EngineDataList {
@JsonProperty("EngineData")
List<EngineData> EngineData;
}
EngineDataList engineDataList = null;
ObjectMapper mapper = new ObjectMapper();
engineDataList = mapper.readValue(myJsonString, new TypeReference<EngineDataList>() {});
英文:
Object can not be mapped to a list,
Refer this for mapping the given json:
public class EngineDataList {
@JsonProperty("EngineData")
List<EngineData> EngineData;
}
EngineDataList engineDataList =null;
ObjectMapper mapper = new ObjectMapper();
engineDataList = mapper.readValue(myJsonString, new TypeReference<EngineDataList>() {});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论