gpt4 book ai didi

java - 使用 Objectmapper JAVA 解析 JSON

转载 作者:行者123 更新时间:2023-11-29 05:33:33 25 4
gpt4 key购买 nike

这是 JSON 字符串:

{
"d":{
"results":[
{
"__metadata":{
"uri":"http://blabla1",
"type":"type1"
},
"Synonym":"miami"
},
{
"__metadata":{
"uri":"http://blabla2",
"type":"type2"
},
"Synonym":"florida"
}
]
}
}

这是代码:

public class Test{        
@JsonIgnoreProperties(ignoreUnknown = true)
public static class d {
public List<Results> results;
public d() {
results = new ArrayList<Results>();
}
public static class Results {
public Metadata __metadata;
public String Synonym;
public Results() {}
}
public static class Metadata {
public String uri;
public String type;
public Metadata() {}
}
}
}

使用以下映射器:

ObjectMapper mapper = new ObjectMapper();
Test.d readValue = mapper.readValue(jsonString, Test.d.class);
for (Test.d.Results k : readValue.results) {
System.out.println("synonym: "+k.Synonym);
}

它没有给我任何错误,只是一个空的结果数组列表......附:我在这段时间做了很多更改,对于给您带来的不便,我们深表歉意

最佳答案

您必须创建一个符合 jSon 答案的对象,像这样(未测试):

class d {

public List<Results> results;

public d() {
}
}

class Results {

public Metadata metadata;
public String synonym;

public Results() {
}
}

class Metadata {

public String uri;
public String type;


public Metadata() {
}
}

希望对您有所帮助!

关于java - 使用 Objectmapper JAVA 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20281562/

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