18

I made a rest call to a service and stored the response in a 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:

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:

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]
1
  • Use UserIdentifier convention instead of userIdentifier to match with class name. I hope it will work Commented Feb 4, 2016 at 20:47

4 Answers 4

21

Figured out what the problem was. Needed to extract the jsonobject instead of getting the string. Here was the line that fixed the issue:

UserIdentifier userIdentifier = gson.fromJson(jsonResponse.getJSONObject("userIdentifier").toString(), UserIdentifier.class);
Sign up to request clarification or add additional context in comments.

1 Comment

Can this not be done using org.Json.JSONObject?
12

You may need use gson

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

Comments

0

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:

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

1 Comment

edited my post to try this solution but still getting an error
-1

Use Gson library

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

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

In your case jsonString is resourceResponse

For details study Gson documentation

2 Comments

I get the error org.json.JSONException: JSONObject["userIdentifier"] not a string. at org.json.JSONObject.getString(JSONObject.java:658)
Are you using Gson now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.