gpt4 book ai didi

java - 如何为嵌套的 JSON 响应映射 Mixins

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:22:55 26 4
gpt4 key购买 nike

我正在使用 Jackson API 将我的 JSON 响应映射到 java 对象。例如,

对于响应 { name :'karthikeyan',age:'24',gender:'Male'}

@JsonProperty("name")
public String _name;
@JsonProperty("age")
public int _age;
@JsonProperty("gender")
public String _gender;

是 Mix-in 并且工作正常。(在内部我们将映射此 pojo 和 Mix-in)。现在我如何在 Mix-in 中表示以下响应?

{
name :'karthikeyan',
age:'24',
gender:'Male',
interest:
{
books:'xxx',
music:'yyy',
movie:'zzz'
}
}

我试过以下方法,但没有成功。

@JsonProperty("name")
public String _name;
@JsonProperty("age")
public int _age;
@JsonProperty("gender")
public String _gender;

@JsonProperty("interest")
public InterestPojo interestPojo; //created same format mix-in and pojo for interest params as well.

但无法准确映射它们,请给出您的意见和想法?

最佳答案

我尝试了以下方法:

    ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(new Something("Name", 12, "male", new Nested("books", "Music", "Movie"))));

public class Something {

@JsonProperty("name")
public String name;
@JsonProperty("age")
public int age;
@JsonProperty("gender")
public String gender;
@JsonProperty("interest")
public Nested nested;
//Constructor
}

public class Nested {

@JsonProperty("books")
public String books;
@JsonProperty("music")
public String music;
@JsonProperty("movie")
public String movie;

//Constructor
}

输出是:

{
"name":"Name",
"age":12,
"gender":"male",
"interest":
{
"books":"books",
"music":"Music",
"movie":"Movie"
}
}

所以一切都按预期工作。我已经检查过如果您提供一些 setter 和 getter 并将字段的可见性设置为私有(private)是否有区别,但这并没有什么不同。

也许您想向我们展示您的 InterestPojo 或您的输出/堆栈跟踪?

编辑:好的,我想我明白了 ;)

我尝试了以下方法:

public void start() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(Something.class, Nested.class);
mapper.getDeserializationConfig().addMixInAnnotations(Something.class, Nested.class);
System.out.println(mapper.writeValueAsString(new Something("Name", 12, "male", new NestedImpl("name", null))));
}
public final class Something {
private final String name;
private int age;
private String gender;
// thats your interest thing
public Nested nested;

public Something(String name, int age, String gender, Nested nested) {
this.name = name;
this.age = age;
this.gender = gender;
this.nested = nested;
}
String getName() {
return name;
}
Nested getNested() {
return nested;
}
}
public abstract class Nested {
@JsonProperty("name-ext")
abstract String getName();
@JsonProperty("interest-ext")
abstract Nested getNested();
}
public class NestedImpl extends Nested {
private String name;
private Nested nested;
private NestedImpl(String name, Nested nested) {
this.name = name;
this.nested = nested;
}
@Override
String getName() {
return name;
}
@Override
Nested getNested() {
return nested;
}
}

输出:

{
"age":12,
"gender":"male",
"name-ext":"Name",
"interest-ext":
{
"name-ext":"name",
"interest-ext":null
}
}

这不完全是您的结构,但我认为这就是您想要的。我说得对吗?

EDIT2:我用 JSON->Object 和 Object->JSON 测试了以下结构。

ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(Something.class, Mixin.class);
mapper.getSerializationConfig().addMixInAnnotations(Nested.class, NestedMixin.class);
mapper.getDeserializationConfig().addMixInAnnotations(Something.class, Mixin.class);
mapper.getDeserializationConfig().addMixInAnnotations(Nested.class, NestedMixin.class);

Nested nested = new Nested();
nested.setName("Nested");
nested.setNumber(12);

Something some = new Something();
some.setName("Something");
some.setAge(24);
some.setGender("Male");
some.setNested(nested);

String json = mapper.writeValueAsString(some);
System.out.println(json);
Something some2 = mapper.readValue(json, Something.class);
System.out.println("Object: " + some2);

public abstract class Mixin {

@JsonProperty("name")
private String _name;
@JsonProperty("age")
private int _age;
@JsonProperty("gender")
private String _gender;
@JsonProperty("interest")
private Nested nested;
}

public class Something {
private String _name;
private int _age;
private String _gender;
private Nested nested;

// You have to provide Setters and Getters!!
}

public abstract class NestedMixin {

@JsonProperty("nameNested")
private String name;
@JsonProperty("numberNested")
private int number;
}

public class Nested {
private String name;
private int number;

// You have to provide Setters and Getters!!
}

输出:{"age":24,"gender":"Male","name":"Something","interest":{"nameNested":"Nested","numberNested":12}}

对象:Something{name=Something, age=24, gender=Male, nested=Nested{name=Nested, number=12}}

注意:jackson 似乎在内部类方面遇到了问题。因此,如果您在额外的项目中测试该示例,则会创建额外的类文件;)

EDIT3:如果您使用的是模块,请尝试以下操作:

public class JacksonMixinModule extends SimpleModule {
public JacksonMixinModule() {
super("JacksonMixinModule", new Version(0, 1, 0, "SNAPSHOT"));
}
@Override
public void setupModule(SetupContext context) {
super.setupModule(context);
context.setMixInAnnotations(Something.class, Mixin.class);
context.setMixInAnnotations(Nested.class, NestedMixin.class);
}
}

...

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JacksonMixinModule());

关于java - 如何为嵌套的 JSON 响应映射 Mixins,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21751615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com