GSON fromJson 返回 null

Tho*_*omQ 8 android json gson

我正在寻找解析 JSONobjects,但在使用 fromJson 时我总是得到 null。我确信 inputStream 是有效的,看起来读取器也已初始化并填充,但“响应”不断返回 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;
Run Code Online (Sandbox Code Playgroud)

搜索响应类:

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;

    }
Run Code Online (Sandbox Code Playgroud)

结果类:

package com.example.places;

import com.google.gson.annotations.SerializedName;

    public class Result {

        @SerializedName("name")
        public String name;

    }
Run Code Online (Sandbox Code Playgroud)

这是 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"
    }
Run Code Online (Sandbox Code Playgroud)

Tho*_*omQ 2

嗯,很奇怪。在本例中,错误来自 SearchResponse 类。如果你查看 JSON,第一个字段是

"html_attributions" : [],
Run Code Online (Sandbox Code Playgroud)

所以是一个数组。

在搜索响应中

public String html_attributions;
Run Code Online (Sandbox Code Playgroud)

应该

public String[] html_attributions;
Run Code Online (Sandbox Code Playgroud)

代码现在显示出生命的迹象。为什么它抛出一个空异常而不是预期的“java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY”,我不知道。