Search code examples
androidjsonjacksongsonpojo

Android:Mapping json values to pojo


I am trying to map JSON values to POJO using GSON. When I try to get object back from JSON string all the variables are set to null.
here is my code

Pojo.java

public class PatientSymptoms {

    private Integer AnalSymptomsMapId;
    private Integer SymptomId;
    private String Symptom;
    private Boolean IsRemoved;
    private Boolean IsSynced;
    private Boolean IsSentToPatient;

    //Getters and Setters
}

Mapping code

JSONObject jsonObject = (JSONObject) jsonArray.get(i);
PatientSymptoms symptoms = new PatientSymptoms();

Gson gson = new Gson();
String x =  gson.toJson(jsonObject);
symptoms = gson.fromJson(x,PatientSymptoms.class);

but PatientSymptoms object values are always null. This is the screen shot of the debugger

debugger o/p

UPDATED JSON RESPONSE

{
  "Success": true,
  "StatusCode": 0,
  "StatusMessage": "",
  "Data": [
    {
      "AnalSymptomsMapId": 250,
      "SymptomId": 95,
      "Symptom": "asdf",
      "IsRemoved": false,
      "IsSynced": false,
      "IsSentToPatient": true
    }
  ]
}

Solution

  • Can you give us an example of the Json? (like an string)

    (JSONObject) jsonArray.get(i).toString()
    

    Anyway, I recomend you to use @SerializedName annotation from Gson to do the mapping:

    https://google.github.io/gson/apidocs/com/google/gson/annotations/SerializedName.html