qq_30101569
qq_30101569
采纳率75%
2016-11-24 04:47 阅读 1.9k
已采纳

GSON解析JAVABEAN返回空值

{
"ErrorMessage": null,
"ResponseData": {
"Data": [
{
"DeviceTypeConfigId": "1",
"DeviceTypeName": "1",
"SensorInfos": [
{
"SensorCode": "AirTemperature",
"SensorConfigId": 1,
"SensorName": "空气温度",
"ValueType": "Value"
},
{
"SensorCode": "AirHumidity",
"SensorConfigId": 2,
"SensorName": "空气湿度",
"ValueType": "Value"
},
{
"SensorCode": "CarbonDioxide",
"SensorConfigId": 3,
"SensorName": "二氧化碳",
"ValueType": "Value"
},
{
"SensorCode": "Radiance",
"SensorConfigId": 4,
"SensorName": "辐射强度",
"ValueType": "Value"
},
{
"SensorCode": "SoilTemperature",
"SensorConfigId": 5,
"SensorName": "土壤温度",
"ValueType": "Value"
},
{
"SensorCode": "SoilHumidity",
"SensorConfigId": 6,
"SensorName": "土壤湿度",
"ValueType": "Value"
}
]
}
],
"Index": 1,
"PageSize": 1,
"TotalCount": 8
}
}
这是服务器返回的JSON数据,在转换的时候我是这样写的

jsonObject = new JSONObject(responseInfo.result);
JsonsRootBean jsonsRootBean=gson.fromJson(responseInfo.result, JsonsRootBean.class);
.Responsedata res=jsonsRootBean.getResponsedata();
System.out.println("--res-----"+res);
控制台中显示res 返回的值为NULL,请问这是为什么呢,实体类我已经写好了

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    u010243044 天蓝风暖 2016-11-24 06:33

    转换之前你先打印一下responseInfo.result有没有数据,如果有数据你再看看你的实体类写的对不对啊。我直接拿的你上面的json数据测试是有数据的。
    图片说明

    点赞 评论 复制链接分享
  • yangjh19930423 却只为一人 2016-11-24 06:21

    实体类有问题,下面是按你JSON数据写的一个实体类,get、set自行添加。

    public class JsonsRootBean {
    private String ErrorMessage;
    private Data ResponseData;

    public class Data {
        private List<DataList> Data;
        private int Index;
        private int PageSize;
        private int TotalCount;
    }
    
    public class DataList {
        private String DeviceTypeConfigId;
        private String DeviceTypeName;
        private List<SensorInfosList> SensorInfos;
    }
    
    public class SensorInfosList {
        private String SensorCode;
        private int SensorConfigId;
        private String SensorName;
        private String ValueType;
    }
    

    }

    点赞 评论 复制链接分享
  • qq_30101569 qq_30101569 2016-11-24 07:15

    public class Sensorinfos {
    private String sensorcode;
    private int sensorconfigid;
    private String sensorname;
    private String valuetype;
    public void setSensorcode(String sensorcode) {
    this.sensorcode = sensorcode;
    }
    public String getSensorcode() {
    return sensorcode;
    }

    public void setSensorconfigid(int sensorconfigid) {
         this.sensorconfigid = sensorconfigid;
     }
     public int getSensorconfigid() {
         return sensorconfigid;
     }
    
    public void setSensorname(String sensorname) {
         this.sensorname = sensorname;
     }
     public String getSensorname() {
         return sensorname;
     }
    
    public void setValuetype(String valuetype) {
         this.valuetype = valuetype;
     }
     public String getValuetype() {
         return valuetype;
     }
    

    }

    点赞 评论 复制链接分享

相关推荐