gpt4 book ai didi

java - 使用 Gson 将 JSON 解析为 Java 类

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

我在使用 GSON 将 JSON 解析为 Java 类时遇到问题。这是 JSON:

{
"results": {
"searchCriteria": {
"start": "2002-01-01",
"end": "2015-07-06",
"query": "test",
"lat": "45.9511",
"lon": "1.4063",
"population": 5000,
"popop": ">",
"range": 50000
},
"citiesDetailed": [{
"lon": 0.90143,
"weight": 1,
"count": 1,
"name": "Saint-Junien",
"lat": 45.88867
},
{
"lon": 1.2578,
"weight": 1,
"count": 405,
"name": "Limoges",
"lat": 45.83153
},
{
"lon": 1.21213,
"weight": 10,
"count": 25789,
"name": "Isle",
"lat": 45.80272
},
{
"lon": 1.86667,
"weight": 1,
"count": 400,
"name": "Gueret",
"lat": 46.16667
},
{
"lon": 1.40063,
"weight": 1,
"count": 2,
"name": "Ambazac",
"lat": 45.95983
}]

}
}

这是我的类(class)。

结果.java

package com.test.classes;

import java.util.List;

public class Results{
private List citiesDetailed;
private SearchCriteria searchCriteria;

public List getCitiesDetailed(){
return this.citiesDetailed;
}
public void setCitiesDetailed(List citiesDetailed){
this.citiesDetailed = citiesDetailed;
}
public SearchCriteria getSearchCriteria(){
return this.searchCriteria;
}
public void setSearchCriteria(SearchCriteria searchCriteria){
this.searchCriteria = searchCriteria;
}
}

搜索条件.java

package com.test.classes;

public class SearchCriteria{
private String end;
private String lat;
private String lon;
private String popop;
private Number population;
private String query;
private Number range;
private String start;

public String getEnd(){
return this.end;
}
public void setEnd(String end){
this.end = end;
}
public String getLat(){
return this.lat;
}
public void setLat(String lat){
this.lat = lat;
}
public String getLon(){
return this.lon;
}
public void setLon(String lon){
this.lon = lon;
}
public String getPopop(){
return this.popop;
}
public void setPopop(String popop){
this.popop = popop;
}
public Number getPopulation(){
return this.population;
}
public void setPopulation(Number population){
this.population = population;
}
public String getQuery(){
return this.query;
}
public void setQuery(String query){
this.query = query;
}
public Number getRange(){
return this.range;
}
public void setRange(Number range){
this.range = range;
}
public String getStart(){
return this.start;
}
public void setStart(String start){
this.start = start;
}
}

CitiesDetailed.java

package com.test.classes;

public class CitiesDetailed{
private Number count;
private Number lat;
private Number lon;
private String name;
private Number weight;

public Number getCount(){
return this.count;
}
public void setCount(Number count){
this.count = count;
}
public Number getLat(){
return this.lat;
}
public void setLat(Number lat){
this.lat = lat;
}
public Number getLon(){
return this.lon;
}
public void setLon(Number lon){
this.lon = lon;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public Number getWeight(){
return this.weight;
}
public void setWeight(Number weight){
this.weight = weight;
}
}

我只是用它来解析它(字符串 json 等于上面的 JSON):

Gson gson = new Gson();
Results r = gson.fromJson(json, Results.class);

System.out.println("Query: " + r.getSearchCriteria().getQuery());

这会导致 NullPointerException,所以显然我的映射在某个地方发生了偏差。我不知道在哪里。

最佳答案

问题是您的 json 中只有一个名为 results 的条目,因此您距离正确解析它还差了一层。

您需要创建一个类来保存返回的Results对象,并使用Gson将您的json解析为那个

例如,您可以像这样创建 ResultWrapper.java:

public class ResultWrapper {
Results results;

public Results getResults() {
return results;
}

public void setResults(Results results) {
this.results = results;
}
}

...然后使您的解析代码如下所示:

    Gson gson = new Gson();
ResultWrapper rw = gson.fromJson(json, ResultWrapper.class);
System.out.println("Query: " + rw.getResults().getSearchCriteria().getQuery());

...你会得到如下输出:

Query: test

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

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