将JSONObject转换为Java对象

16

我向一个服务发起了REST调用并将响应存储在一个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以匹配类名。希望它能够正常工作。 - Noor Nawaz
4个回答

20

找出问题所在,需要提取json对象而不是获取字符串。以下是解决问题的代码行:

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

这个不能使用 org.Json.JSONObject 来完成吗? - Shubs

12

你可能需要使用gson

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

0

问题在于你不能像这样转换在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);

我编辑了我的帖子尝试这个解决方案,但仍然出现错误。 - Richard

-1
使用Gson库
Gson gson=new GsonBuilder().create();

UserIdentifier userIdentifier=gson.fromJson(jsonString,UserIdentifier.class);

在您的情况下,jsonString 是 resourceResponse

有关详细信息,请参阅 Gson 文档


我收到了错误信息 org.json.JSONException: JSONObject["userIdentifier"] not a string. at org.json.JSONObject.getString(JSONObject.java:658) - Richard
你现在正在使用Gson吗? - Noor Nawaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接