gpt4 book ai didi

java - JSON 编码/解码包含对象列表的对象(Java、JSON/Jackson)

转载 作者:行者123 更新时间:2023-12-01 06:08:24 25 4
gpt4 key购买 nike

我认为这将是一个简单的问题。

我有一个简单的结构,一个类“Persons”,其中包含“SimplePerson”对象的列表。 Persons.java 看起来像这样:

package jsontests;

import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;

@JsonRootName (value="persons")
public class Persons {

@JsonProperty("person")
private List< SimplePerson> l;

public Persons(List<SimplePerson> pl) {
this.l = pl;
}

public Persons() {
this.l = new ArrayList<>();
}

public List<SimplePerson> getL() {
return this.l;
}

public void setL(List<SimplePerson> l) {
this.l = l;
}
}

“SimplePerson.java”看起来像这样:

package jsontests;
import com.fasterxml.jackson.annotation.JsonRootName;

@JsonRootName (value="person")
public class SimplePerson {

String name;
String firstname;


/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the firstname
*/
public String getFirstname() {
return firstname;
}

/**
* @param firstname the firstname to set
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}
}

我使用此代码片段来创建这些对象并编码它们:

package jsontests;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonListTest {

public static void main(String[] args) {
JsonListTest var = new JsonListTest();
var.run();
}

private void run() {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
Persons cl = new Persons();

SimplePerson first = new SimplePerson();
first.setName("Schmidt");
first.setFirstname("Peter");
cl.getL().add(first);
SimplePerson second = new SimplePerson();
second.setName("Smith");
second.setFirstname("George");

cl.getL().add(second);

String s = null;
try {
s = mapper.writeValueAsString(cl);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
System.out.println(s);
}
}

结果符合预期:

{
"persons" : {
"person" : [ {
"name" : "Schmidt",
"firstname" : "Peter"
}, {
"name" : "Smith",
"firstname" : "George"
} ]
}
}

在我看来,它是“我们有一个名为 Person 的对象,其中包含一个名为 person 的对象。这个 person-object 包含一个由名称/名字对组成的对象数组。”所有这一切对我来说完全没问题,这正是代码的内容

不幸的是,我从客户那里收到的东西看起来有点不同。我收到了

{
"persons": [
{
"person": {
"name" : "Schmidt",
"firstname": "Peter"
}
},{
"person": {
"name" : "Smith",
"firstname": "George"
}
}
]
}

在我看来,这读起来有点不同:“我们有一个名为 Person 的对象,它包含一个数组。该数组的第一个和第二个元素是名为 person 的对象,它们由名称/名字对组成。

相同,但不同:-)我花了一天时间寻找一种方法来消耗我得到的输入 - 但我没有成功。更改我的类结构不会有任何问题 - 说服客户提供不同 JSON 的机会很小。

有什么想法吗?谢谢!

最佳答案

我遵循 Derick 的建议(参见第一条评论)并生成了所需的数据模型。这似乎是最好的解决方案。

关于java - JSON 编码/解码包含对象列表的对象(Java、JSON/Jackson),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39973367/

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