[英]Converting JSONObject to Java Object
I made a rest call to a service and stored the response in a JSONObject
.我对服务进行了休息调用,并将响应存储在JSONObject
。 However, I am trying to convert it to a class object and getting errors.但是,我正在尝试将其转换为类对象并出现错误。 Here's my code:这是我的代码:
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");
Here's what the response looks like:下面是响应的样子:
{
"userIdentifier": {
"uid": "ee63a52cda7bf411dd8603ac196951aa77",
"code": "63a5297e7bf411dd8603ac196951aa77",
"retailId": "860658787",
"pointOfEntry": "RETAIL"
},
"resultCode": true
}
And here's what the UserIdentifier
class looks like:这是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;
}
}
But I get the error:但我收到错误:
java.lang.ClassCastException: org.json.JSONObject cannot be cast to app.identity.UserIdentifier
What am I doing wrong?我究竟做错了什么?
Edit 1: Here's the attempt at using gson from the answers:编辑 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);
But I get the error:但我收到错误:
org.json.JSONException: JSONObject["userIdentifier"] not a string.
at org.json.JSONObject.getString(JSONObject.java:658) ~[json-20140107.jar:na]
Figured out what the problem was.想通了是什么问题。 Needed to extract the jsonobject instead of getting the string.需要提取 jsonobject 而不是获取字符串。 Here was the line that fixed the issue:这是解决问题的行:
UserIdentifier userIdentifier = gson.fromJson(jsonResponse.getJSONObject("userIdentifier").toString(), UserIdentifier.class);
You may need use gson您可能需要使用 gson
class Name{
String resultCode;
UserIdentifier useridentifier;
//getters
}
Gson gson=new Gson();
Name name=gson.fromJson(jsonString,Name.class);
the things is that you can't cast the object that return in the get method like this, One solution could be this, using GSON
library:问题是你不能像这样转换在 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);
Use Gson library使用 Gson 库
Gson gson=new GsonBuilder().create();
UserIdentifier userIdentifier=gson.fromJson(jsonString,UserIdentifier.class);
In your case jsonString is resourceResponse在你的情况下 jsonString 是resourceResponse
For details study Gson documentation有关详细信息,请研究Gson 文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.