2016-12-15 3 views
-1

Ich habe eine JSON und seine Hauptklasse in meinem Android Projekt. Wenn ich versuche, Daten mit GSON zu analysieren, erhalte ich den Fehler unten. Alle Daten, die ich extrahiere, habe ich einen Fehler. Vorschläge? Vielen Dank im VorausParsen von JSON-Objekt mit Gson gibt Null Wert

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

-Code

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); 

Das ist mein JSON Array

{ 
"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 Klasse

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; 
       } 
      } 
     } 
    } 
} 
} 

Antwort

0

Alles, was Sie benötigen sollten, ist zu tun (vorausgesetzt jsonString enthält das JSON Sie oben enthalten).

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

Ihre Aufgabe oben würde sich ändern, auch zu:

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

+0

Die Json nicht voll ist. Im 'Device' Array gibt es weitere Elemente. Wegen der Länge entferne ich einige Teile. 'test3 netsClass = new Gson(). Aus Json (jsonString, test3.class);' gibt mir 'Erwartete BEGIN_OBJECT aber war BEGIN_ARRAY' –

+0

Kannst du den vollen json (oder vielleicht einen Link dazu) posten? Selbst wenn es mehr Elemente im 'Net'-Array gibt, sollten Sie' test3' direkt beim Aufruf von 'fromJson' verwenden können. –

+0

[link] (http://pastebin.com/BtsMF8Ee) –