以Jackson读取JSON为对象列表。

huangapple go评论75阅读模式
英文:

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 engineDataList = null;
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&lt;EngineData&gt;`

I wrote the following but it doesn&#39;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&lt;EngineData&gt; EngineData;
   //...
}

It will work with bellow JSON :

[
    {
      &quot;make&quot;: &quot;Toyota&quot;,
      &quot;transmission&quot;: &quot;Manual&quot;,
      &quot;fuel&quot;: &quot;Petrol&quot;
    },
    {
      &quot;make&quot;: &quot;GMC&quot;,
      &quot;transmission&quot;: &quot;Auto&quot;,
      &quot;fuel&quot;: &quot;Petrol&quot;
    },
    {
      &quot;make&quot;: &quot;Honda&quot;,
      &quot;transmission&quot;: &quot;Manual&quot;,
      &quot;fuel&quot;: &quot;Petrol&quot;
    }
  ]

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(&quot;EngineData&quot;)
   List&lt;EngineData&gt; EngineData;
}

EngineDataList engineDataList =null;
ObjectMapper mapper = new ObjectMapper();
engineDataList = mapper.readValue(myJsonString, new TypeReference&lt;EngineDataList&gt;() {});

huangapple
  • 本文由 发表于 2020年8月21日 22:45:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63525035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定