将JSONObject转换为Java对象
我对服务进行了rest调用,并将响应存储在将JSONObject转换为Java对象,java,json,spring,rest,jsonobject,Java,Json,Spring,Rest,Jsonobject,我对服务进行了rest调用,并将响应存储在JSONObject中。但是,我试图将其转换为类对象,并得到错误。这是我的密码: RestOperations operations = /*initalize*/; String body = /*build request body*/; String resourceResponse = operations.postForObject(/* url */, body, String.class); JSONObject jsonResponse
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
}
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;
}
}
下面是UserIdentifier
类的外观:
{
"userIdentifier": {
"uid": "ee63a52cda7bf411dd8603ac196951aa77",
"code": "63a5297e7bf411dd8603ac196951aa77",
"retailId": "860658787",
"pointOfEntry": "RETAIL"
},
"resultCode": true
}
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
org.json.JSONException: JSONObject["userIdentifier"] not a string.
at org.json.JSONObject.getString(JSONObject.java:658) ~[json-20140107.jar:na]
我做错了什么
编辑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);
但我得到了一个错误:
java.lang.ClassCastException: org.json.JSONObject cannot be cast to app.identity.UserIdentifier
org.json.JSONException: JSONObject["userIdentifier"] not a string.
at org.json.JSONObject.getString(JSONObject.java:658) ~[json-20140107.jar:na]
使用Gson库
Gson gson=new GsonBuilder().create();
UserIdentifier userIdentifier=gson.fromJson(jsonString,UserIdentifier.class);
在您的情况下,jsonString是resourceResponse
对于详细信息研究问题是,您不能像这样强制转换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);
您可能需要使用gson
class Name{
String resultCode;
UserIdentifier useridentifier;
//getters
}
找出问题所在。需要提取jsonobject而不是获取字符串。以下是解决问题的一行:
UserIdentifier userIdentifier = gson.fromJson(jsonResponse.getJSONObject("userIdentifier").toString(), UserIdentifier.class);
我得到的错误
org.json.JSONException:JSONObject[“userIdentifier”]不是字符串。在org.json.JSONObject.getString(JSONObject.java:658)
上,您现在正在使用Gson吗?编辑了我的帖子以尝试此解决方案,但仍然得到一个errorUse UserIdentifier约定,而不是UserIdentifier来匹配类名。我希望它能起作用