将具有多个条目的JSON映射到数组

时间:2022-03-15 15:36:06

I have a JSON file with multiple entries that have same attribute names, but different attribute values, such as:

我有一个JSON文件,其中有多个条目具有相同的属性名称,但属性值不同,例如:

{
  "name" : { "first" : "A", "last" : "B" },
  "gender" : "MALE",
  "married" : false,
  "noOfChildren" : 2
},
{
  "name" : { "first" : "C", "last" : "D" },
  "gender" : "FEMALE",
  "married" : true,
  "noOfChildren" : 1
}

The class that it should be mapped is:

它应该映射的类是:

public class Human {

公共类人类{

private Name name;
private String gender;
private int age;

<getter, setters etc>

}

}

EDIT: Service code is :

编辑:服务代码是:

List<Human> humans = null;
ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

try {

    humans= objectMapper.readValue(json, new TypeReference<List<Human>>(){});

} catch (IOException e) {
    e.printStackTrace();
}

JSON is parsed from HTTP entity and with correct format and now I added the annotation ass suggested in the answers.

JSON是从HTTP实体解析的,格式正确,现在我添加了答案中建议的注释屁股。

As you can see, they have some attributes in common, but differ in others, and I would like to map those common fields. Is it possible to map the JSON this way ? I have tried mapping JSON to a collection/list/array of JsonNodes, but I keep getting erros about deserialization, while mapping only one instance of JSON entry works just fine. Is there another way of doing this ?

正如您所看到的,它们有一些共同的属性,但在其他属性上有所不同,我想映射这些常见字段。是否可以通过这种方式映射JSON?我已经尝试将JSON映射到JsonNodes的集合/列表/数组,但我不断获得关于反序列化的错误,而映射只有一个JSON条目的实例工作得很好。还有另一种方法吗?

2 个解决方案

#1


1  

Use

使用

@JsonIgnoreProperties(ignoreUnknown = true)
public class Human {
    private Name name;
    private String gender;
    // getters, settets, default constructor
}

Or if you are using Lombok then it will be

或者,如果您使用的是Lombok,那么它将是

@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Human {
    private Name name;
    private String gender;
}

#2


1  

use

使用

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

while deserializing json to POJO class.

同时将json反序列化为POJO类。

The JSON you have provide in question will give following error, as it is not a valid one.

您提供的JSON将提供以下错误,因为它不是有效的错误。

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

Valid Json would be like this:

有效的Json会是这样的:

[
  {
    "name": {
      "first": "A",
      "last": "B"
    },
    "gender": "MALE",
    "married": false,
    "noOfChildren": 2
  },
  {
    "name": {
      "first": "C",
      "last": "D"
    },
    "gender": "FEMALE",
    "married": true,
    "noOfChildren": 1
  }
]

#1


1  

Use

使用

@JsonIgnoreProperties(ignoreUnknown = true)
public class Human {
    private Name name;
    private String gender;
    // getters, settets, default constructor
}

Or if you are using Lombok then it will be

或者,如果您使用的是Lombok,那么它将是

@Data
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Human {
    private Name name;
    private String gender;
}

#2


1  

use

使用

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

while deserializing json to POJO class.

同时将json反序列化为POJO类。

The JSON you have provide in question will give following error, as it is not a valid one.

您提供的JSON将提供以下错误,因为它不是有效的错误。

Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

Valid Json would be like this:

有效的Json会是这样的:

[
  {
    "name": {
      "first": "A",
      "last": "B"
    },
    "gender": "MALE",
    "married": false,
    "noOfChildren": 2
  },
  {
    "name": {
      "first": "C",
      "last": "D"
    },
    "gender": "FEMALE",
    "married": true,
    "noOfChildren": 1
  }
]