Gson返回空值

时间:2019-01-28 10:32:29

标签: android gson

我正在尝试使用gson库转换以下响应,但是我得到的是null值,有人可以帮我解决我做错了什么吗?我在gradle中使用implementation 'com.google.code.gson:gson:2.3.1'

JSON response
----

    {
      "Status": "success",
      "access_token": "512b2dee-fa64-43c5-83c6-f5bb2fe42b7e",
      "refresh_token": "8339069d-0fe7-4d83-9053-584693cf846a",
      "role": "admin",
      "locationName": "Hyderabad",
      "Message": "Successfully login",
      "scope": "read write trust",
      "name": "Test Admin",
      "location": 1,
      "token_type": "bearer",
      "expires_in": "11139"
    }

    my object
    ---

      private String Status;
      private String access_token;
      private String refresh_token;
      private String role;
      private String Message;
      private int location;
      private String locationName;
      private String name;

Gson parsing
---
  Gson gson = new GsonBuilder().serializeNulls().create();
  LoginObject myElement = gson.fromJson(response, LoginObject.class);
   System.out.println("des--->"+myElement.getAccess_token());

1 个答案:

答案 0 :(得分:0)

使用此功能:

Gson gson = new Gson();

代替此:

Gson gson = new GsonBuilder().serializeNulls().create();



private String Status;
      private String access_token;
      private String refresh_token;
      private String role;
      private String message;
      private int location;
      private String locationName;
      private String name;

在您使用String Message的对象中,请将Message更改为message