web-dev-qa-db-fra.com

Conversion de JSONObject en objet Java

J'ai fait un appel de repos à un service et ai stocké la réponse dans une JSONObject. Cependant, j'essaye de le convertir en objet de classe et j'obtiens des erreurs. Voici mon 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");

Voici à quoi ressemble la réponse:

{
  "userIdentifier": {
    "uid": "ee63a52cda7bf411dd8603ac196951aa77",
    "code": "63a5297e7bf411dd8603ac196951aa77",
    "retailId": "860658787",
    "pointOfEntry": "RETAIL"
  },
  "resultCode": true
}

Et voici à quoi ressemble la classe 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;
    }
}

Mais je reçois l'erreur:

Java.lang.ClassCastException: org.json.JSONObject cannot be cast to app.identity.UserIdentifier

Qu'est-ce que je fais mal?

Edit 1: Voici la tentative d'utilisation de gson à partir des réponses:

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

Mais je reçois l'erreur:

org.json.JSONException: JSONObject["userIdentifier"] not a string.
    at org.json.JSONObject.getString(JSONObject.Java:658) ~[json-20140107.jar:na]
6
Richard

Compris quel était le problème. Nécessaire pour extraire le jsonobject au lieu de récupérer la chaîne. Voici la ligne qui a résolu le problème:

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

Vous pouvez avoir besoin d'utiliser gson 

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

}

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

Utiliser la bibliothèque Gson

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

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

Dans votre cas, jsonString est resourceResponse

Pour plus de détails Documentation Gson

0
Noor Nawaz

le problème est que vous ne pouvez pas convertir l'objet renvoyé dans la méthode get de la manière suivante. Une solution pourrait être la suivante, en utilisant la bibliothèque 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);
0
fernandojne