GSON fromJson返回null

时间:2014-03-24 03:31:15

标签: android json gson

我正在寻找解析JSONobjects,但是在使用fromJson时我一直都是null。我确信inputStream是有效的,看起来读者也被初始化并填充,但是'response'不断返回null,导致致命的异常。我希望得到3个字段的“响应”,其中一个是结果类型列表,结果中唯一的字段是名称。我究竟做错了什么?

我正在使用以下代码:

    String url = uribuilder.build().toString();

    try {
        HttpRequest httpRequest = requestFactory.buildGetRequest(new GenericUrl(url));
        HttpResponse httpResponse = httpRequest.execute();
        inputStream = httpResponse.getContent();

    } catch (IOException e) {
        e.printStackTrace();
    }

    Gson gson = new Gson();
    Reader reader = new InputStreamReader(inputStream);
    SearchResponse response = gson.fromJson(reader, SearchResponse.class);
    List<Result> resultList = response.results;

SearchResponse类:

package com.example.places;

import com.google.gson.annotations.SerializedName;
import java.util.List;

    public class SearchResponse {

        @SerializedName("html_attributions")
        public String html_attributions;

        public List<Result> results;

        @SerializedName("status")
        public String status;

    }

结果类:

package com.example.places;

import com.google.gson.annotations.SerializedName;

    public class Result {

        @SerializedName("name")
        public String name;

    }

这是JSON inputStream的一个例子,我已经确认它正在var inputStream中下载。

    "html_attributions" : [],
    "results" : [
    {
    "geometry" : {
    "location" : {
    "lat" : 52.3784713,
    "lng" : 4.629422400000001
    }
    },
    "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
    "id" : "426ed4299daa6451e2293b1677e06b524562c547",
    "name" : "Lange Lakenstraat",
    "reference" : "CqQBoAAAANEj46iTeMbRHeUcEGFeaCAMrnHxXvFpadEefjrl4qDBitY4b5c2kjVunQrm496UeU1BHLiflo4tA6z7sDBZw3u0b2oPwTqOSiA1Jf4TZA3J6GeGfo_0tLV5dnHH2a3gJbl7fnDvWdZco2BvP_mgVSJgcC2hnb3H8xf9_HYUKtWjAPiV-lY-TnIeZqJaAaH7rJfg9OuHMjmYVYnYuaW0FoQSEEBP1JORpML1X0D9qgGPl0QaFJhSaAVENN_I4-p8tK-5B790QkwD",
    "types" : [ "route" ],
    "vicinity" : "Oude Stad"
    },
    {
    "geometry" : {
    "location" : {
    "lat" : 52.3812134,
    "lng" : 4.633607599999999
    },
    "viewport" : {
    "northeast" : {
    "lat" : 52.3854271,
    "lng" : 4.644829
    },
    "southwest" : {
    "lat" : 52.375918,
    "lng" : 4.624482899999999
    }
    }
    },
    "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
    "id" : "7b7289c46ec49e2de4c7922bb1489f4b7d285385",
    "name" : "Centrum",
    "reference" : "CpQBjQAAAPTn0HELVmsIds40sY_RGXIY1GmrhqlfejMmrQrG2Gl095VujXOugcPR5ZuZ3-aNZhLZEXNsOO_Ghf_0vEnIkjVan11tb1WtDiwJrIfAa31hPt8XlIGY3JBWKXew0qVpGXZbEoHvhThzn-z0OBZ0pqMR5PZrU7mgoH26pbAR_y-Nngo74sQHZs9wO3dzQl34RRIQMvDj3KMrzUBgSf6WVVHzNRoUlDtZh_8FsLn3qaEWzvPnnyFe9j8",
    "types" : [ "sublocality", "political" ],
    "vicinity" : "Centrum"
    }
    ],
    "status" : "OK"
    }

2 个答案:

答案 0 :(得分:1)

嗯,奇怪的一个。在这种情况下,错误来自SearchResponse类。如果你看一下JSON,第一个字段是

"html_attributions" : [],

所以一个数组。

在SearchResponse

public String html_attributions;

应该是

public String[] html_attributions;

现在代码显示了生命的迹象。为什么它抛出了一个null异常而不是一个预期的&#34; java.lang.IllegalStateException:期望一个字符串,但是BEGIN_ARRAY&#34;,我不知道。

答案 1 :(得分:0)

我遇到了类似的问题,但就我而言,我有这个对象:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EmployeeDto {
    @ApiModelProperty(notes = "Crew Id")
    private String crewId;

    @ApiModelProperty(notes = "Employee Role")
    private String employeeRole;

    @ApiModelProperty(notes = "Employee First Name")
    private String firstName;

    @ApiModelProperty(notes = "Employee Last Name")
    private String lastName;
}

这是来自上一个查询的字符串:

{"crewId":"924142","employeeRole":"Manager","firstName":"Hans","lastName":"Miller"}

问题是因为camelCase记法,所以我改成:

{"crew_id":"924142","employee_role":"Manager","first_name":"Hans","last_name":"Miller"}

根据documentation可以在Gson中定义字段策略:

Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();