2017-09-26 3 views
-1

J'ai une question à propos de GSON lib.(android, JAVA) Comment mettre en œuvre 3 ou 4 profondeur JSON avec GSON?

Mes données JSON est ICI:

{ 
    "UserID":"", 
    "Data1":{ 
     "Data1_1":[{ 
      "name":"name1", 
      "test":"test2" 
     }, 
     { 
      "name":"name2", 
      "test":"test2" 
     }], 
     "Data1_2":{ 
      "Data1_2_1":[{ 
       "addr":"addr1", 
       "phone":"phone1" 
      }, 
      { 
       "addr":"addr2", 
       "phone":"phone2" 
      }], 
      "Data1_2_2":[{ 
       "country":"country1", 
       "sex":"M" 
      }, 
      { 
       "country":"country1", 
       "sex":"F" 
      }] 
     } 
    } 
} 

Mon GSON classe d'objets est ICI:

public class GsonClass { 
    public String UserID; 
    public ArrayList<Data1> Data1; 

    public class Data1 { 
     ArrayList<Data1_1> Data1_1; 
     ArrayList<Data1_2> Data1_2; 

     public class Data1_1 { 
      public String name; 
      public String test; 
     } 

     public class Data1_2 { 
      ArrayList<Data1_2_1> Data1_2_1; 
      ArrayList<Data1_2_2> Data1_2_2; 

      public class Data1_2_1 { 
       public String addr; 
       public String phone; 
      } 

      public class Data1_2_2 { 
       public String country; 
       public String sex; 
      } 
     } 
    } 
} 

Mais, bibliothèque GSON retourne JSONParserException .. Quand je test sans Data1_2, GSON Parser était D'ACCORD. Mais, avec Data1_2. Les exceptions de retour de la bibliothèque GSON.

Comment définir la classe GSON dans JSON à 3 ou 4 profondeurs?

+1

Que dit le message d'exception? Qu'est-ce que tu ne comprends pas? Est-ce que ce sont vraiment des classes internes dans vos définitions POJO? Pourquoi? –

Répondre

1

Essayez ceci.

Tout d'abord, nous devons savoir comment traiter avec [] et {} dans le Gson.

  • Si nous trouvons {} dans le JSON, nous devons utiliser Databean dans le Gson.

  • Si nous trouvons [] dans le JSON, nous devons utiliser List<DataBean> dans le Gson.

Et c'est la classe.

public class GsonClass { 

/** 
* UserID : 
* Data1 : {"Data1_1":[{"name":"name1","test":"test2"},{"name":"name2","test":"test2"}],"Data1_2":{"Data1_2_1":[{"addr":"addr1","phone":"phone1"},{"addr":"addr2","phone":"phone2"}],"Data1_2_2":[{"country":"country1","sex":"M"},{"country":"country1","sex":"F"}]}} 
*/ 

private String UserID; 
private Data1Bean Data1; 

public String getUserID() { 
    return UserID; 
} 

public void setUserID(String UserID) { 
    this.UserID = UserID; 
} 

public Data1Bean getData1() { 
    return Data1; 
} 

public void setData1(Data1Bean Data1) { 
    this.Data1 = Data1; 
} 

public static class Data1Bean { 
    /** 
    * Data1_1 : [{"name":"name1","test":"test2"},{"name":"name2","test":"test2"}] 
    * Data1_2 : {"Data1_2_1":[{"addr":"addr1","phone":"phone1"},{"addr":"addr2","phone":"phone2"}],"Data1_2_2":[{"country":"country1","sex":"M"},{"country":"country1","sex":"F"}]} 
    */ 

    private Data12Bean Data1_2; 
    private List<Data11Bean> Data1_1; 

    public Data12Bean getData1_2() { 
     return Data1_2; 
    } 

    public void setData1_2(Data12Bean Data1_2) { 
     this.Data1_2 = Data1_2; 
    } 

    public List<Data11Bean> getData1_1() { 
     return Data1_1; 
    } 

    public void setData1_1(List<Data11Bean> Data1_1) { 
     this.Data1_1 = Data1_1; 
    } 

    public static class Data12Bean { 
     private List<Data121Bean> Data1_2_1; 
     private List<Data122Bean> Data1_2_2; 

     public List<Data121Bean> getData1_2_1() { 
      return Data1_2_1; 
     } 

     public void setData1_2_1(List<Data121Bean> Data1_2_1) { 
      this.Data1_2_1 = Data1_2_1; 
     } 

     public List<Data122Bean> getData1_2_2() { 
      return Data1_2_2; 
     } 

     public void setData1_2_2(List<Data122Bean> Data1_2_2) { 
      this.Data1_2_2 = Data1_2_2; 
     } 

     public static class Data121Bean { 
      /** 
      * addr : addr1 
      * phone : phone1 
      */ 

      private String addr; 
      private String phone; 

      public String getAddr() { 
       return addr; 
      } 

      public void setAddr(String addr) { 
       this.addr = addr; 
      } 

      public String getPhone() { 
       return phone; 
      } 

      public void setPhone(String phone) { 
       this.phone = phone; 
      } 
     } 

     public static class Data122Bean { 
      /** 
      * country : country1 
      * sex : M 
      */ 

      private String country; 
      private String sex; 

      public String getCountry() { 
       return country; 
      } 

      public void setCountry(String country) { 
       this.country = country; 
      } 

      public String getSex() { 
       return sex; 
      } 

      public void setSex(String sex) { 
       this.sex = sex; 
      } 
     } 
    } 

    public static class Data11Bean { 
     /** 
     * name : name1 
     * test : test2 
     */ 

     private String name; 
     private String test; 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     public String getTest() { 
      return test; 
     } 

     public void setTest(String test) { 
      this.test = test; 
     } 
    } 
} 
} 

Et utilisez dans votre code comme ceci.

Et comment analyser JSON dans le code.

1.Utiliser gson.fromJson(response,GsonClass.class); pour convertir JSON en DataBean classe.

2.Utilisez get et définissez la méthode .Comme ceci gsonClass.getUserID().

3.Quand nous trouvons List dedans, nous pouvons utiliser la boucle for .Et la boucle for, nous pouvons utiliser comme gsonClass.getData1().getData1_1().get(i).getName();.

Gson gson = new Gson(); 
GsonClass gsonClass = gson.fromJson(response,GsonClass.class); 

String UserID = gsonClass.getUserID(); 

for (int i = 0; i < gsonClass.getData1().getData1_1().size(); i++) { 
    String name = gsonClass.getData1().getData1_1().get(i).getName(); 
    String test = gsonClass.getData1().getData1_1().get(i).getTest(); 
} 

for (int i = 0; i < gsonClass.getData1().getData1_2().getData1_2_1().size(); i++) { 
    String addr = gsonClass.getData1().getData1_2().getData1_2_1().get(i).getAddr(); 
    String phone = gsonClass.getData1().getData1_2().getData1_2_1().get(i).getPhone(); 
} 

for (int i = 0; i < gsonClass.getData1().getData1_2().getData1_2_2().size(); i++) { 
    String country = gsonClass.getData1().getData1_2().getData1_2_2().get(i).getCountry(); 
    String sex = gsonClass.getData1().getData1_2().getData1_2_2().get(i).getSex(); 
} 

Et vous pouvez utiliser GsonFormat dans votre Android Studio pour convertir JSON classe TO.

Ce sera rapide pour vous.

1.Add GsonFormat enter image description here 2.Utiliser dans le code

enter image description here enter image description here

+0

Veuillez expliquer ce que vous avez changé et pourquoi. –

+0

Je l'avais expliqué à ce sujet @ SotiriosDelimanolis – KeLiuyue

+0

@KeLiuyue Merci pour votre aimable réponse monsieur :) Je vais essayer cela. Merci :-) –