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

在Android中使用JAVA从Bean类获取空值?,java,android,json,gson,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 d

下面是我的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实例2050:23:::0:::0:::null:::null

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

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

您可以使用:

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

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

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

添加代码后可能出现重复的错误:12-31 04:32:35.355:W/System.err9637:com.google.gson.JsonSyntaxException:2012-09-16 05:11:23 12-31 04:32:35.360:W/System.err9637:at com.google.gson.internal.bind.DateTypeAdapter.DeserializeTodateTypeAdapter.java:81 12-31 04:32:35.360:W/System.err9637:at com.google.gson.internal.bind.DateTypeAdapter.readDateTypeAdapter.java:66 12-31 04:32:35.360:W/System.err9637:at com.google.gson.internal.bind.DateTypeAdapter.readDateTypeAdapter.java:41
Gson gson = new GsonBuilder()
    .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
    .setDateFormat("yyyy-MM-dd HH:mm:ss")
    .create();