2016-11-17 144 views
0

我试图使用GSONJSON值映射到POJO。当我尝试从JSON字符串中获取对象时,所有变量都设置为null。
这里是我的代码Android:将json值映射到pojo

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 
} 

映射代码

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

PatientSymptoms对象的值总是空。这是调试器

debugger o/p

修订 JSON响应

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

实现Parcelable。 – Nidhi

+0

发布你的json请 – Alexander

+0

回复已更新 –

回答

0

让您Pojo.java的屏幕截图作为

public class PatientSymptoms { 

    private int AnalSymptomsMapId; 
    private int SymptomId; 
    private String Symptom; 
    private boolean IsRemoved; 
    private boolean IsSynced; 
    private boolean IsSentToPatient; 

    //Getters and Setters 
} 

所以,默认值将是那些的原始类型。

0

我建议让你的模型中使用this

  1. 选择来源类型:JSON
  2. 注释风格:Gson
  3. 点击预览。

然后,您已为您的JSON生成模型类。

而且不要忘记实施Parcelable

+0

对于这样一个简短的答案,请评论它! – AndiGeeky

+0

不幸的是,我必须有50的声望来评论@AndiGeeky –

+0

哦,你可以从该链接复制一些内容,然后让答案更具描述性! – AndiGeeky

0

我建议你尝试​​

模型

public class PatientSymptoms { 

     private int AnalSymptomsMapId; 
     private int SymptomId; 
     private String Symptom; 
     private boolean IsRemoved; 
     private boolean IsSynced; 
     private boolean IsSentToPatient; 

    //Getters and Setters 
} 

用法

Ion.with(context) 
    .load("http://example.com/api/patient") 
    .as(new TypeToken<List<PatientSymptoms>>(){}) 
    .setCallback(new FutureCallback<List<PatientSymptoms>>() { 
     @Override 
     public void onCompleted(Exception e, List<PatientSymptoms> symptoms) { 
      // Use the list of Patient Symtoms 
    } 
}); 

注意:你需要记住的唯一的事情是JSON键应该匹配的POJO或MODEL中的变量名称。