gpt4 book ai didi

java - 使用 Jackson 从 POJO 创建 JSON,但其结构不符合要求

转载 作者:行者123 更新时间:2023-12-01 21:11:59 25 4
gpt4 key购买 nike

我想在java中创建一个具有此结构的JSON文件

"FieldA" : {
Preis: "100,00 €",
Text_de: "some german",
Text_en: "engl",
Materialnummer: "32400020",
},
"FieldB" : {
Preis: "100,00 €",
Text_de: "foo",
Text_en: "bar",
Materialnummer: "32400020",
}

我使用 jackson 为此创建了一个 POJO

public class IpadField {

public String fieldname;
public String Text_de;
public String Text_en;
public String Preis;
public String Materialnummer;

public IpadField(){

}

@Override
public String toString() {
return "IpadField{" +
"fieldname='" + fieldname + '\'' +
", Text_de='" + Text_de + '\'' +
", Text_en='" + Text_en + '\'' +
", Preis='" + Preis + '\'' +
", Materialnummer='" + Materialnummer + '\'' +
'}';
}

public IpadField(String fieldname, String text_de, String text_en, String preis, String materialnummer) {
this.fieldname = fieldname;
Text_de = text_de;
Text_en = text_en;
Preis = preis;
Materialnummer = materialnummer;
}

}

使用 Jackson 创建这些字段的列表时

public void createJson() throws Exception {
File outFile = new File("d://test.json");
if (outFile.exists()){
outFile.delete();
}

IpadField fieldaA = new IpadField("fieldA", "test", "test", "test", "test");
IpadField fieldaB = new IpadField("fieldB", "test", "test", "test", "test");
Assert.assertFalse(outFile.exists());
JsonCreator.createForField(outFile, fieldaA );
ArrayList<IpadField> fieldList = new ArrayList<IpadField>();
fieldList.add(fieldaA);
fieldList.add(fieldaB);

JsonCreator.createForList(outFile, fieldList);
Assert.assertTrue(outFile.exists());

这会产生

[
{
"Materialnummer": "test",
"Preis": "test",
"Text_de": "test",
"Text_en": "test",
"fieldname": "fieldA"
},
{
"Materialnummer": "test",
"Preis": "test",
"Text_de": "test",
"Text_en": "test",
"fieldname": "fieldB"
}
]

这不是所需的结构,知道我需要如何调整 POJO 以便它产生正确的结构吗?谢谢

最佳答案

首先,您的要求不是有效的 JSON 结构。应该是这样的:

{
"FieldA": {
"Preis": "100,00 €",
"Text_de": "some german",
"Text_en": "engl",
"Materialnummer": "32400020"
},
"FieldB": {
"Preis": "100,00 €",
"Text_de": "foo",
"Text_en": "bar",
"Materialnummer": "32400020"
}
}
  • 整个数据的括号
  • 所有键都用双引号括起来
  • 最后一个值不能包含逗号。

如果这是您的要求,请将 POJO 更改为如下所示:

import com.fasterxml.jackson.annotation.JsonIgnore;

public class IpadField {

@JsonIgnore
public String fieldname;
public String Text_de;
public String Text_en;
public String Preis;
public String Materialnummer;

public IpadField() {

}

@Override
public String toString() {
return "IpadField{" + "fieldname='" + fieldname + '\'' + ", Text_de='" + Text_de + '\'' + ", Text_en='"
+ Text_en + '\'' + ", Preis='" + Preis + '\'' + ", Materialnummer='" + Materialnummer + '\'' + '}';
}

public IpadField(String fieldname, String text_de, String text_en, String preis, String materialnummer) {
this.fieldname = fieldname;
Text_de = text_de;
Text_en = text_en;
Preis = preis;
Materialnummer = materialnummer;
}

}

即在 fieldName 上添加 @JsonIgnore,因为您不希望它位于结构中。

那么它应该是这样的:

    public void createJSON(){
File outFile = new File("d://test.json");
if (outFile.exists()){
outFile.delete();
}
IpadField fieldaA = new IpadField("fieldA", "test", "test", "test", "test");
IpadField fieldaB = new IpadField("fieldB", "test", "test", "test", "test");
Map<String, IpadField> addIpadMap = addIpad(null, fieldaA);
addIpadMap = addIpad(addIpadMap, fieldaB);
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());
writer.writeValue(outFile, addIpadMap);
}

public Map<String, IpadField> addIpad(Map<String, IpadField> map, IpadField ipadField) {
if (map == null)
map = new HashMap<String, IpadField>();
map.put(ipadField.fieldname, ipadField);
return map;
}

它会给出类似这样的输出:

{
"fieldA" : {
"Text_de" : "test",
"Text_en" : "test",
"Preis" : "test",
"Materialnummer" : "test"
},
"fieldB" : {
"Text_de" : "test",
"Text_en" : "test",
"Preis" : "test",
"Materialnummer" : "test"
}
}

关于java - 使用 Jackson 从 POJO 创建 JSON,但其结构不符合要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40985098/

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