2013-08-30 6 views
-1

Je suis nouveau aux services Web et j'utilise la bibliothèque gson pour la première fois. Je suis capable d'analyser certaines chaînes JSON simples mais je suis coincé ici. Ceci est ma réponseJson analyse avec Gson avec des données et des tableaux

{"message":"Action completed successfully.","results":3,"action":"param","data":[{"title":"test1","id":2,"completeCount":0},{"title":"test2","id":3,"completeCount":0},{"title":"test2","id":3,"completeCount":0}],"success":true} 

Voilà comment je suis en train de l'analyser

Gson gson = new Gson(); 
Chracter character = gson.fromJson(successResponse, Character.class); 

Et voici ma classe

public class Character { 


private List<Detail> data = new ArrayList<Detail>(); 
private String action ; 
private int results; 
private String message; 
private Detail detail; 


public Character() { 

} 

public Character(List<Detail> data, String action, int results, 
     String message) { 
    super(); 
    this.data = data; 
    this.action = action; 
    this.results = results; 
    this.message = message; 
} 

public List<Detail> getData() { 
    return data; 
} 

public void setData(List<Detail> data) { 
    this.data = data; 
} 

public String getAction() { 
    return action; 
} 

public void setAction(String action) { 
    this.action = action; 
} 

public int getResults() { 
    return results; 
} 

public void setResults(int results) { 
    this.results = results; 
} 

public String getMessage() { 
    return message; 
} 

public void setMessage(String message) { 
    this.message = message; 
} 


public Detail getDetail() { 
    return detail; 
} 

public void setDetail(Detail detail) { 
    this.detail = detail; 
} 

public class Detail{ 

    private String title; 
    private int id; 
    private int completeCount ; 


    public Detail() { 

    } 

    public Detail(String title, int id, int completeCount) { 
     super(); 
     this.title = title; 
     this.id = id; 
     this.completeCount = completeCount; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public int getId() { 
     return Id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public int getCompleteCount() { 
     return completeCount; 
    } 

    public void setCompleteCount(int completeCount) { 
     this.completeCount = completeCount; 
    } 




} 





} 
+0

http://stackoverflow.com/questions/5314813/json-gson-fromjson-java-objects –

+0

pouvez-vous s'il vous plaît expliquer comment utiliser les classes internes dans de tels cas – Ravi

+0

@Chintan pas son pas résolu, je faisais déjà ce qui a été donné dans le lien ci-dessus et je peux voir la liste de l'intérieur a 3 éléments mais les variables de classe interne ont toujours une valeur nulle – Ravi

Répondre

2

Essayez la classe suivante pour la réponse.

public class Character { 

    /** 
    * Take array of class when you found "[","]" inside json response 
    */ 
    ArrayList<Data> data; 

    /** 
    * take normal values as members 
    */ 
    String action; 
    int results; 
    String message; 
    boolean success; 

    /** 
    * When you will use array, don't forgot to implement empty contructor, 
    * which initialize that array 
    */ 
    public Character() { 
     data = new ArrayList<Character.Data>(); 
    } 

    /** 
    * There will be only "get" methods, no setter method used in GSON bean 
    * 
    * @return 
    */ 
    public String getAction() { 
     return action; 
    } 

    public String getMessage() { 
     return message; 
    } 

    public int getResults() { 
     return results; 
    } 

    public boolean getSuccess() { 
     return success; 
    } 

    public ArrayList<Data> getData(){ 
      return data; 
    } 

    @Override 
    public String toString() { 
     String str = "Action : " + action + "\nMessage : " + message 
       + "\nResults : " + results + "\nSuccess : " + success; 
     str += "\n"; 
     for (Data d : data) { 
      str += "\nTitle : " + d.getTitle(); 
      str += "\nComplete Count : " + d.getCompleteCount(); 
      str += "\nId : " + d.getId(); 
     } 
     return str; 
    } 

    /** 
    * as you have => "data":[...] => in your json response, you need to create 
    * a class for that 
    */ 
    public class Data { 

     /** 
     * take normal values as members 
     */ 
     private String title; 
     private int id; 
     private int completeCount; 

     public String getTitle() { 
      return title; 
     } 

     public int getId() { 
      return id; 
     } 

     public int getCompleteCount() { 
      return completeCount; 
     } 
    } 

} 

Sortie

Pour imprimer la sortie, utilisez le code suivant. J'ai override toString() fonction.

Gson gson = new Gson(); 
Character character = gson.fromJson(strResponse, Character.class); 
Log.d("Home", character.toString()); 

08-30 15: 36: 59,279: DEBUG/Accueil (10022): Action:
08-30 param 15: 36: 59,279: DEBUG/Accueil (10022): Message: Action terminée avec succès.
08-30 15: 36: 59,279: DEBUG/Accueil (10022): Résultats: 3
08-30 15: 36: 59,279: DEBUG/Accueil (10022): Succès: true
08-30 15:36 : 59.279: DEBUG/Accueil (10022): Titre: test1
08-30 15: 36: 59.279: DEBUG/Accueil (10022): Nombre total: 0
08-30 15: 36: 59.279: DEBUG/Accueil (10022): Id: 2
08-30 15: 36: 59.279: DEBUG/Accueil (10022): Titre: test2
08-30 15: 36: 59.279: DEBUG/Accueil (10022): Nombre total: 0
08-30 15: 36: 59.279: DEBUG/Accueil (10022): Id: 3
08-30 15: 36: 59.279: DEBUG/Accueil (1 0022): Titre: test2
08-30 15: 36: 59.279: DEBUG/Home (10022): Nombre total: 0
08-30 15: 36: 59.279: DEBUG/Accueil (10022): Id: 3

+0

@Ravi J'ai essayé, et je reçois ** test1 ** comme sortie .. il y a un problème avec votre code. revérifier. :) –

+0

vous êtes juste merci l'homme pour l'aide et l'explication son fonctionnement maintenant – Ravi

+0

@ Ravi, heureux d'aider .. :) vote sera un grand plaisir ...: D –