在Android中使用JAVA从Bean类中获取空值?

时间:2013-12-30 12:17:15

标签: java android json gson

这是我的Bean类:

public class MenuFields {
    private int id;
    private int menuId;
    private int fieldTypeId;
    private String c4wCode;
    private String fieldLabel;
    private String fieldValues;
    private Date dateCreated;
    private Date dateModified;
    private int isRequired;
    private int isStatic;
    private int fieldOrder;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getMenuId() {
        return menuId;
    }
    public void setMenuId(int menuId) {
        this.menuId = menuId;
    }
    public int getFieldTypeId() {
        return fieldTypeId;
    }
    public void setFieldTypeId(int fieldTypeId) {
        this.fieldTypeId = fieldTypeId;
    }
    public String getC4wCode() {
        return c4wCode;
    }
    public void setC4wCode(String c4wCode) {
        this.c4wCode = c4wCode;
    }
    public String getFieldLabel() {
        return fieldLabel;
    }
    public void setFieldLabel(String fieldLabel) {
        this.fieldLabel = fieldLabel;
    }
    public String getFieldValues() {
        return fieldValues;
    }
    public void setFieldValues(String fieldValues) {
        this.fieldValues = fieldValues;
    }
    public Date getDateCreated() {
        return dateCreated;
    }
    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }
    public Date getDateModified() {
        return dateModified;
    }
    public void setDateModified(Date dateModified) {
        this.dateModified = dateModified;
    }
    public int getIsRequired() {
        return isRequired;
    }
    public void setIsRequired(int isRequired) {
        this.isRequired = isRequired;
    }
    public int getIsStatic() {
        return isStatic;
    }
    public void setIsStatic(int isStatic) {
        this.isStatic = isStatic;
    }
    public int getFieldOrder() {
        return fieldOrder;
    }
    public void setFieldOrder(int fieldOrder) {
        this.fieldOrder = fieldOrder;
    }
}

我在字符串中硬编码了我的JSON:

public class MainActivity extends Activity {
    String jsonObjectString ="[\n\n\n {\n \"id\": 22,\n \"menu_id\": 1,\n \"field_type_id\": 1,\n \"c4w_code\": \"1234\",\n \"field_label\": \"Customer No\",\n \"field_values\": \"\",\n \"date_Created\": \"2012-09-16 05:11:23\",\n \"date_modified\": \"2013-11-20 10:33:23\",\n \"is_required\": 0,\n \"is_static\": 1,\n \"field_order\": 1\n },\n {\n \"id\": 23,\n \"menu_id\": 1,\n \"field_type_id\": 1,\n \"c4w_code\": \"1234\",\n \"field_label\": \"Company Name\",\n \"field_values\": \"\",\n \"date_Created\": \"2012-09-16 05:11:56\",\n \"date_modified\": \"2013-11-20 10:33:23\",\n \"is_required\": 1,\n \"is_static\": 1,\n \"field_order\": 3\n }\n\n]";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = this;
  }

  private void getCustomerMenuFieldsData(String parameter){  
    try{  
      MenuFields[]  menuFieldHolder = gson.fromJson(jsonObjectString,MenuFields[].class);
      Log.i("menuField Instance ",""+menuFieldHolder[1].getId() +"::::" +         menuFieldHolder[1].getIsRequired() +":::" + menuFieldHolder[1].getIsStatic() +":::" +menuFieldHolder[1].getC4wCode() +":::" +menuFieldHolder[1].getFieldLabel());
    }  
    catch (Throwable t){
      t.printStackTrace();
    }   
}

这是LogCat: 12-31 04:02:36.950:I / menuField Instance(2050):23 :::: 0 ::: 0 ::: null ::: null

我的代码有什么问题?为什么它只提供int值而所有String值都是Null? 我一次又一次地检查了代码它浪费了我大约7个小时,但我仍然在String中得到空值。

1 个答案:

答案 0 :(得分:0)

您需要将bean成员映射到json键。

您可以使用:

Gson gson = new GsonBuilder()
    .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
    .create();

或者:

@SerializedName("c4w_code")
private String c4wCode;

对于您的错误,您需要提供日期格式:

Gson gson = new GsonBuilder()
    .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
    .setDateFormat("yyyy-MM-dd HH:mm:ss")
    .create();