来自JSON的GSON返回空值

bvuwiixz  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(215)

我想解析JSON对象,但是在使用fromJson时总是得到空值。我确信inputStream是有效的,它看起来像是读取器已经初始化并填充了,但是“response”总是返回空值,这导致了一个致命的异常。我期望得到一个有3个字段的“response”,其中一个是结果类型列表,结果中唯一的字段是名称。我做错了什么?
下面是一个例子:

  1. String url = uribuilder.build().toString();
  2. try {
  3. HttpRequest httpRequest = requestFactory.buildGetRequest(new GenericUrl(url));
  4. HttpResponse httpResponse = httpRequest.execute();
  5. inputStream = httpResponse.getContent();
  6. } catch (IOException e) {
  7. e.printStackTrace();
  8. }
  9. Gson gson = new Gson();
  10. Reader reader = new InputStreamReader(inputStream);
  11. SearchResponse response = gson.fromJson(reader, SearchResponse.class);
  12. List<Result> resultList = response.results;

搜索响应类:

  1. package com.example.places;
  2. import com.google.gson.annotations.SerializedName;
  3. import java.util.List;
  4. public class SearchResponse {
  5. @SerializedName("html_attributions")
  6. public String html_attributions;
  7. public List<Result> results;
  8. @SerializedName("status")
  9. public String status;
  10. }

结果类别:

  1. package com.example.places;
  2. import com.google.gson.annotations.SerializedName;
  3. public class Result {
  4. @SerializedName("name")
  5. public String name;
  6. }

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

  1. "html_attributions" : [],
  2. "results" : [
  3. {
  4. "geometry" : {
  5. "location" : {
  6. "lat" : 52.3784713,
  7. "lng" : 4.629422400000001
  8. }
  9. },
  10. "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
  11. "id" : "426ed4299daa6451e2293b1677e06b524562c547",
  12. "name" : "Lange Lakenstraat",
  13. "reference" : "CqQBoAAAANEj46iTeMbRHeUcEGFeaCAMrnHxXvFpadEefjrl4qDBitY4b5c2kjVunQrm496UeU1BHLiflo4tA6z7sDBZw3u0b2oPwTqOSiA1Jf4TZA3J6GeGfo_0tLV5dnHH2a3gJbl7fnDvWdZco2BvP_mgVSJgcC2hnb3H8xf9_HYUKtWjAPiV-lY-TnIeZqJaAaH7rJfg9OuHMjmYVYnYuaW0FoQSEEBP1JORpML1X0D9qgGPl0QaFJhSaAVENN_I4-p8tK-5B790QkwD",
  14. "types" : [ "route" ],
  15. "vicinity" : "Oude Stad"
  16. },
  17. {
  18. "geometry" : {
  19. "location" : {
  20. "lat" : 52.3812134,
  21. "lng" : 4.633607599999999
  22. },
  23. "viewport" : {
  24. "northeast" : {
  25. "lat" : 52.3854271,
  26. "lng" : 4.644829
  27. },
  28. "southwest" : {
  29. "lat" : 52.375918,
  30. "lng" : 4.624482899999999
  31. }
  32. }
  33. },
  34. "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
  35. "id" : "7b7289c46ec49e2de4c7922bb1489f4b7d285385",
  36. "name" : "Centrum",
  37. "reference" : "CpQBjQAAAPTn0HELVmsIds40sY_RGXIY1GmrhqlfejMmrQrG2Gl095VujXOugcPR5ZuZ3-aNZhLZEXNsOO_Ghf_0vEnIkjVan11tb1WtDiwJrIfAa31hPt8XlIGY3JBWKXew0qVpGXZbEoHvhThzn-z0OBZ0pqMR5PZrU7mgoH26pbAR_y-Nngo74sQHZs9wO3dzQl34RRIQMvDj3KMrzUBgSf6WVVHzNRoUlDtZh_8FsLn3qaEWzvPnnyFe9j8",
  38. "types" : [ "sublocality", "political" ],
  39. "vicinity" : "Centrum"
  40. }
  41. ],
  42. "status" : "OK"
  43. }
jaxagkaj

jaxagkaj1#

好吧,奇怪的一个。在这个例子中,错误来自SearchResponse类。如果你看一下JSON,第一个字段是

  1. "html_attributions" : [],

所以一个数组。
在搜索响应中

  1. public String html_attributions;

应该是

  1. public String[] html_attributions;

代码现在显示出了生命的迹象。为什么它抛出了一个空异常而不是预期的“java.lang.IllegalStateException:应该是一个字符串,但实际上是开始_ARRAY“,我不知道。

fcipmucu

fcipmucu2#

我也面临着类似的问题,但我的情况是:

  1. @Data
  2. @Builder
  3. @NoArgsConstructor
  4. @AllArgsConstructor
  5. public class EmployeeDto {
  6. @ApiModelProperty(notes = "Crew Id")
  7. private String crewId;
  8. @ApiModelProperty(notes = "Employee Role")
  9. private String employeeRole;
  10. @ApiModelProperty(notes = "Employee First Name")
  11. private String firstName;
  12. @ApiModelProperty(notes = "Employee Last Name")
  13. private String lastName;
  14. }

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

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

这个问题是因为camelCase符号,所以我将它改为:

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

documentation相对应的那一字段的策略可以在Gson中定义:

  1. Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
展开查看全部

相关问题