gpt4 book ai didi

java - 将 Bing JSON 转换为 Java

转载 作者:行者123 更新时间:2023-12-02 00:34:56 26 4
gpt4 key购买 nike

我有一个如下所示的 JSON 对象:

{
"SearchResponse":{
"Version":"2.2",
"Query":{
"SearchTerms":"codexperiments"
},
"Web":{
"Total":41,
"Offset":0,
"Results":[
{
"Title":"Code Xperiments - Because IT is an experimental science",
"Description":"The deferred-time page scrolling technique I described in my previous article is not what I really wanted to achieve at first. Although powerful, it lacks of “dynamism”.",
"Url":"http:\/\/www.codexperiments.com\/",
"CacheUrl":"http:\/\/cc.bingj.com\/cache.aspx?q=codexperiments&d=4548825798150827&mkt=en-US&w=a8960869,c9182d07",
"DisplayUrl":"www.codexperiments.com",
"DateTime":"2011-01-14T16:19:00Z"
}
]
}
}
}

我正在尝试使用 gson 解析标题/url

public class GoogleResults {

private ResponseData responseData;
public ResponseData getResponseData() { return responseData; }
public void setResponseData(ResponseData responseData) { this.responseData = responseData; }
public String toString() { return "ResponseData[" + responseData + "]"; }

static class ResponseData {
private List<Result> results;
public List<Result> getResults() { return results; }
public void setResults(List<Result> results) { this.results = results; }
public String toString() { return "Results[" + results + "]"; }
}

static class Result {
private String url;
private String title;
public String getUrl() { return url; }
public String getTitle() { return title; }
public void setUrl(String url) { this.url = url; }
public void setTitle(String title) { this.title = title; }
public String toString() { return "Results[url:" + url +",title:" + title + "]"; }
}

}

GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);

但我的结果始终为空。我缺少什么想法吗??

最佳答案

确保数据结构与JSON数据结构匹配,否则无法正确解析。另外,就像其他回答者所说的那样,以 JSON 中的名称命名事物。例如,如果变量在 JSON 中大写,请记住将其大写。

public class Result {
SR SearchResponse;

static class SR {
W Web;

static class W {
List<R> Results;

static class R {
public String Url;
public String Title;

public String toString() {
return Url + Title;
}
}
}
}

public String toString() {
return SearchResponse.Web.Results.toString();
}
}

关于java - 将 Bing JSON 转换为 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8053313/

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