JSON Jackson 将多个键反序列化为同一字段 [英] JSON Jackson deserialization multiple keys into same field

查看:26
本文介绍了JSON Jackson 将多个键反序列化为同一字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 JSON 转换为 POJO.我曾与 Jackson 合作转换标准 JSON 文件.在这种特殊情况下,我想将键值覆盖为默认"类/变量.在这种情况下,要替换的键值有多个(即数百个,要替换的键值未知).

I am trying to convert JSON into POJO. I have worked with Jackson to convert standard JSON file. In this particular case, I would like to overwrite the key value to "default" class/variable. In this case, there are multiple key value to be replaced (ie. hundreds, and the key values to be replaced are unknown).

这可能吗?我想将它存储到 Map 中,然后迭代并将每个存储到 POJO 中,但想知道是否有不同的选择,因为我对将 JSON 存储到 Map 不太熟悉.

Is this possible? I thought of storing it into Map, then iterate and store each into POJO, but wondering if there is different option, since I am not that familiar with storing JSON to Map.

要处理的 JSON 示例:

Example of the JSON to be processed:

"People" : {
    "person1" : { 
      "name" : "john doe",
      "address" : "123 main st",
      "email" : "john@doe.com"
    },
    "person2" : { 
      "name" : "bob cat",
      "address" : "234 dog st",
      "email" : "bob@cat.com"
    },
    "person3" : { 
      "name" : "foo bar",
      "address" : "111  1st ave",
      "email" : "foo@bar.com"
    },
    "person8" : { 
      "name" : "james bono",
      "address" : "999 alaska st",
      "email" : "james@bono.com"
    }
}

是否可以生成以下结构的类?主要问题是有数百个值要替换,假设它们未知,我不能使用这种方法.

Is it possible to generate the class in the following structure? The main issue is there are hundreds of value to be replaced and assuming they are unknown, I can't use this approach.

@JsonIgnoreProperties(ignoreUnknown = true)
public class People { 

  @JsonAlias({"person1", "person2"})
  private List<Details> person; // --> this should be the default replacing person1, person2, and so on 

  private class Details { 
    String name;
    String address;
    String email;
  }
}

推荐答案

您可以使用 JsonAnySetter 注释,用于所有属性 personXYZ.见下例:

You can use JsonAnySetter annotation for all properties personXYZ. See below example:

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);

        System.out.println(mapper.readValue(jsonFile, People.class).getPersons());
    }
}

class People {

    private List<Details> persons = new ArrayList<>();

    @JsonAnySetter
    public void setPerson(String name, Details person) {
        this.persons.add(person);
    }

    public List<Details> getPersons() {
        return persons;
    }

    public static class Details {
        String name;
        String address;
        String email;

        // getters, setters, toString
    }
}

对于您的 JSON 上面的代码打印:

For your JSON above code prints:

[Details{name='john doe', address='123 main st', email='john@doe.com'}, Details{name='bob cat', address='234 dog st', email='bob@cat.com'}, Details{name='foo bar', address='111  1st ave', email='foo@bar.com'}, Details{name='james bono', address='999 alaska st', email='james@bono.com'}]

如果您使用内部类,请记住将其设为 public static 以使其对 Jackson 实例化过程可见.

In case you use inner class remember to make it public static to make it visible to Jackson instantiation process.

另见:

这篇关于JSON Jackson 将多个键反序列化为同一字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆