2016-02-04 117 views
4

我對服務進行了休息呼叫,並將響應存儲在JSONObject中。但是,我試圖將其轉換爲類對象並獲取錯誤。這裏是我的代碼:將JSONObject轉換爲Java對象

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] 
+0

使用UserIdentifier的約定,而不是UserIdentifier的匹配與類名。我希望它能工作 –

回答

3

找出問題所在。需要提取jsonobject而不是獲取字符串。這裏是固定的問題行:

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

使用GSON庫

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

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

在你的情況jsonString是resourceResponse使用GSON

詳細研究Gson documentation

+0

我得到錯誤'org.json.JSONException:JSONObject [「userIdentifier」]不是一個字符串。 \t at org.json.JSONObject.getString(JSONObject.java:658)' – Richard

+0

你現在在使用Gson嗎? –

+0

我編輯了我的帖子 – Richard

0

的事情是,你可以不投,在這樣的get方法返回的對象,一種解決方案可能是這樣,:

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); 
+0

編輯了我的帖子來嘗試這個解決方案,但仍然出現錯誤 – Richard

2

您可能需要使用gson

class Name{ 
String resultCode; 
UserIdentifier useridentifier; 
//getters 

} 

Gson gson=new Gson(); 
Name name=gson.fromJson(jsonString,Name.class);