-1
私はJSON
とそのメインクラスをAndroid
プロジェクトに持っています。 GSON
でデータを解析しようとすると、以下のエラーが表示されます。 抽出したデータですが、何らかのエラーがあります。 提案?事前に感謝JSONオブジェクトをGsonで解析するとNull値が返されます
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);
は、これはあなたがする必要がある必要がありますすべてが(jsonString
が含まれていると仮定している私の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クラス
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;
}
}
}
}
}
}
。 'Device'配列には他の要素があります。 長さのため、一部を取り除きます。 'test3 netsClass = newGson().Json(jsonString、test3.class); ' は私に'期待BEGIN_OBJECTだがBEGIN_ARRAY'を返す –
完全なjsonを投稿できますか? 'Net'配列にもっと多くの要素があっても、' fromJson'を呼び出すときに 'test3'を直接使うことができます。 –
[リンク](http://pastebin.com/BtsMF8Ee) –