使用Gson解析JSON对象返回Null值

时间:2016-12-15 19:30:10

标签: android json gson

我在JSON项目中使用了Android及其主要课程。当我尝试使用GSON解析数据时,我收到以下错误。 我提取的任何数据,我都有一些错误。 建议?提前致谢

Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference

代码

test3[] netsClass = new Gson().fromJson(String.valueOf(jsonObj),test3[].class);
String objstr = netsClass[1].getNet().get(1).getDevice().get(1).getPlants().getPlant1().getDescription();
Log.d(TAG, "onCreate: STATUS "+ objstr);

这是我的JSON数组

{
"Net": [
    {
        "-IdAliasZone": "id1",
        "-FullDomain": "domain",
        "-PubIp": "IP",
        "-RemMode": "0",
        "Device": [
            {
                "-IdAliasDevice": "id2",
                "-Description": "device2",
                "-IdType": "1",
                "-Type": "2",
                "-LocalUrl": "ip"
            },
            {
                "-IdAliasDevice": "id3",
                "-Description": "device3",
                "-IdType": "2",
                "-Type": "64",
                "-LocalUrl": "url",
                "-RemotePort": "33",
                "-Mac": "00:00:00:00",
                "-Status": "asd",
                "-Version": "1",
                "-Uptime": "1234",
                "Plants": {
                    "Plant1": {
                        "-Description": "1",
                        "-Enable": "1"
                    },
                    "Plant2": {
                        "-Description": "2",
                        "-Enable": "1"
                    },
                    "Plant3": {
                        "-Description": "3",
                        "-Enable": "1"
                    },
                    "Plant4": {
                        "-Description": "4",
                        "-Enable": "1"
                    }
                }
            }
        ]
    }
    ]
 }

test3 Class

public class test3 implements Serializable {

private List<NetClass> Net;

public List<NetClass> getNet() {
    return Net;
}

public void setNet(List<NetClass> Net) {
    this.Net = Net;
}

public static class NetClass {
    @SerializedName("-IdAliasZone")
    private String IdAliasZone;
    @SerializedName("-FullDomain")
    private String FullDomain;
    @SerializedName("-PubIp")
    private String PubIp;
    @SerializedName("-RemMode")
    private String RemMode;


    private List<DeviceClass> Device;

    public String getIdAliasZone() {
        return IdAliasZone;
    }

    public void setIdAliasZone(String IdAliasZone) {
        this.IdAliasZone = IdAliasZone;
    }

    public String getFullDomain() {
        return FullDomain;
    }

    public void setFullDomain(String FullDomain) {
        this.FullDomain = FullDomain;
    }

    public String getPubIp() {
        return PubIp;
    }

    public void setPubIp(String PubIp) {
        this.PubIp = PubIp;
    }

    public String getRemMode() {
        return RemMode;
    }

    public void setRemMode(String RemMode) {
        this.RemMode = RemMode;
    }

    public List<DeviceClass> getDevice() {
        return Device;
    }

    public void setDevice(List<DeviceClass> Device) {
        this.Device = Device;
    }

    public static class DeviceClass {
        @SerializedName("-IdAliasDevice")
        private String IdAliasDevice;
        @SerializedName("-Description")
        private String Description;
        @SerializedName("-IdType")
        private String IdType;
        @SerializedName("-Type")
        private String Type;
        @SerializedName("-LocalUrl")
        private String LocalUrl;
        @SerializedName("-RemotePort")
        private String RemotePort;
        @SerializedName("-Mac")
        private String Mac;
        @SerializedName("-Status")
        private String Status;
        @SerializedName("-Version")
        private String Version;
        @SerializedName("-Uptime")
        private String Uptime;


        private PlantsClass Plants;

        public String getIdAliasDevice() {
            return IdAliasDevice;
        }

        public void setIdAliasDevice(String IdAliasDevice) {
            this.IdAliasDevice = IdAliasDevice;
        }

        public String getDescription() {
            return Description;
        }

        public void setDescription(String Description) {
            this.Description = Description;
        }

        public String getIdType() {
            return IdType;
        }

        public void setIdType(String IdType) {
            this.IdType = IdType;
        }

        public String getType() {
            return Type;
        }

        public void setType(String Type) {
            this.Type = Type;
        }

        public String getLocalUrl() {
            return LocalUrl;
        }

        public void setLocalUrl(String LocalUrl) {
            this.LocalUrl = LocalUrl;
        }

        public String getRemotePort() {
            return RemotePort;
        }

        public void setRemotePort(String RemotePort) {
            this.RemotePort = RemotePort;
        }

        public String getMac() {
            return Mac;
        }

        public void setMac(String Mac) {
            this.Mac = Mac;
        }

        public String getStatus() {
            return Status;
        }

        public void setStatus(String Status) {
            this.Status = Status;
        }

        public String getVersion() {
            return Version;
        }

        public void setVersion(String Version) {
            this.Version = Version;
        }

        public String getUptime() {
            return Uptime;
        }

        public void setUptime(String Uptime) {
            this.Uptime = Uptime;
        }

        public PlantsClass getPlants() {
            return Plants;
        }

        public void setPlants(PlantsClass Plants) {
            this.Plants = Plants;
        }

        public static class PlantsClass {


            private Plant1Class Plant1;
            private Plant1Class Plant2;
            private Plant1Class Plant3;
            private Plant1Class Plant4;

            public Plant1Class getPlant1() {
                return Plant1;
            }

            public void setPlant1(Plant1Class Plant1) {
                this.Plant1 = Plant1;
            }

            public Plant1Class getPlant2() {
                return Plant2;
            }

            public void setPlant2(Plant1Class Plant2) {
                this.Plant2 = Plant2;
            }

            public Plant1Class getPlant3() {
                return Plant3;
            }

            public void setPlant3(Plant1Class Plant3) {
                this.Plant3 = Plant3;
            }

            public Plant1Class getPlant4() {
                return Plant4;
            }

            public void setPlant4(Plant1Class Plant4) {
                this.Plant4 = Plant4;
            }

            public static class Plant1Class {
                @SerializedName("-Description")
                private String Description;
                @SerializedName("-Enable")
                private String Enable;

                public String getDescription() {
                    return Description;
                }

                public void setDescription(String Description) {
                    this.Description = Description;
                }

                public String getEnable() {
                    return Enable;
                }

                public void setEnable(String Enable) {
                    this.Enable = Enable;
                }
            }
        }
    }
}
 }

1 个答案:

答案 0 :(得分:0)

您需要做的就是(假设jsonString包含您上面包含的json)。

test3 netsClass = new Gson().fromJson(jsonString, test3.class);

您上面的作业也会更改为:

String objstr = netsClass.getNet().get(0).getDevice().get(1).getPlants().getPlant1().getDescription();