当数据是字符串而不是对象时,如何在android中使用解析相同的json响应?

Mah*_*esh 1 android json observable retrofit2

当我得到回应时,成功课程完美无缺.但是当它失败时它给了我错误.

在URL中传递失败数据时的响应:

{
  "success": -1,
  "message": "Invalid username or password",
  "data": ""
}
Run Code Online (Sandbox Code Playgroud)

正确数据在URL中传递时的响应:

{
  "success": 1,
  "message": "User successfully logged in",
  "data": {
    "userId": 219,
    "userName": "mp",
    "picture": "219.png",
    "isBo": false,
    "isPo": true,
    "isHm": true,
    "hmProfileCompletionStatus": 3,
    "poProfileCompletionStatus": 2,
    "poPhoto": null,
    "hmPhoto": null,
    "custStripeToken": "xxx",
    "accountStripeToken": "xx",
    "bgcStatus": true,
    "idCardStatus": true,
    "isInsured": null,
    "amISponsered": false,
    "hmRating": null,
    "poRating": null,
    "email": "xxx@gmail.com"
  }
}
Run Code Online (Sandbox Code Playgroud)

UserResponse.java

@Getter
@Setter
public class UserResponse{
    private int success;
    private String message;
    private User data;
}
Run Code Online (Sandbox Code Playgroud)

错误

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 64 path $.data
Run Code Online (Sandbox Code Playgroud)

Kin*_*ses 5

这不可能.在你的情况下,它应该是这样的

{
  "success": -1,
  "message": "Invalid username or password",
  "data": {}
}
Run Code Online (Sandbox Code Playgroud)

请您的后端服务团队使用空对象更改它,以便可以重复使用相同的响应对象