【问题标题】:Converting JSONObject to Java Object将 JSONObject 转换为 Java 对象
【发布时间】:2016-05-14 14:22:45
【问题描述】:

我对服务进行了休息调用,并将响应存储在JSONObject 中。但是,我正在尝试将其转换为类对象并出现错误。这是我的代码:

RestOperations operations = /*initalize*/;
String body = /*build request body*/;
String resourceResponse = operations.postForObject(/* url */, body, String.class);
JSONObject jsonResponse = new JSONObject(resourceResponse);
UserIdentifier userIdentifier = (UserIdentifier) jsonResponse.get("userIdentifier");

下面是响应的样子:

{
  "userIdentifier": {
    "uid": "ee63a52cda7bf411dd8603ac196951aa77",
    "code": "63a5297e7bf411dd8603ac196951aa77",
    "retailId": "860658787",
    "pointOfEntry": "RETAIL"
  },
  "resultCode": true
}

UserIdentifier 类如下所示:

public class UserIdentifier {
    private String uid;
    private String code;
    private String retailId;

    public UserIdentifier() {

    }

    public UserIdentifier(String uid, String code, String retailId) {
        this.uid = uid;
        this.code = code;
        this.retailId = retailId;
    }

    public String getuid() {
        return uid;
    }

    public void setuid(String uid) {
        this.uid = uid;
    }

    public String getcode() {
        return code;
    }

    public void setcode(String code) {
        this.code = code;
    }

    public String getretailId() {
        return retailId;
    }

    public void setretailId(String retailId) {
        this.retailId = retailId;
    }
}

但我得到了错误:

java.lang.ClassCastException: org.json.JSONObject cannot be cast to app.identity.UserIdentifier

我做错了什么?

编辑 1:这是从答案中使用 gson 的尝试:

Gson gson = new GsonBuilder().create();
String body = /*build request body*/;
String resourceResponse = operations.postForObject(/* url */, body, String.class);
JSONObject jsonResponse = new JSONObject(resourceResponse);
UserIdentifier userIdentifier = gson.fromJson(jsonResponse.getString("userIdentifier"), UserIdentifier.class);

但我得到了错误:

org.json.JSONException: JSONObject["userIdentifier"] not a string.
    at org.json.JSONObject.getString(JSONObject.java:658) ~[json-20140107.jar:na]

【问题讨论】:

  • 使用 UserIdentifier 约定而不是 userIdentifier 来匹配类名。我希望它会工作

标签: java json spring rest jsonobject


【解决方案1】:

你可能需要使用 gson

class Name{
    String resultCode;
    UserIdentifier useridentifier;
    //getters
    
}
Gson gson=new Gson();
Name name=gson.fromJson(jsonString,Name.class);

【讨论】:

    【解决方案2】:

    找出问题所在。需要提取 jsonobject 而不是获取字符串。这是解决问题的行:

    UserIdentifier userIdentifier = gson.fromJson(jsonResponse.getJSONObject("userIdentifier").toString(), UserIdentifier.class);
    

    【讨论】:

    • 不能用org.Json.JSONObject吗?
    【解决方案3】:

    问题是您不能像这样强制转换在 get 方法中返回的对象,一种解决方案可能是这样,使用 GSON 库:

    RestOperations operations = /*initalize*/;
    String body = /*build request body*/;
    Gson gson = new Gson();
    String resourceResponse = operations.postForObject(/* url */, body, String.class);
    JSONObject jsonResponse = new JSONObject(resourceResponse);
    String jsonUserIdentifier = jsonResponse.getString("userIdentifier");
    UserIdentifier userIdentifier = gson.fromJson(jsonUserIdentifier , UserIdentifier.class);
    

    【讨论】:

    • 编辑了我的帖子以尝试此解决方案,但仍然出现错误
    【解决方案4】:

    使用 Gson 库

    Gson gson=new GsonBuilder().create();
    
    UserIdentifier userIdentifier=gson.fromJson(jsonString,UserIdentifier.class);
    

    在你的情况下 jsonString 是 resourceResponse

    详情学习Gson documentation

    【讨论】:

    • 我收到错误org.json.JSONException: JSONObject["userIdentifier"] not a string. at org.json.JSONObject.getString(JSONObject.java:658)
    • 你现在在用 Gson 吗?
    猜你喜欢
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多