J'ai essayé de le faire dans un autre projet, et selon les tutoriels que j'ai vu, je sais que je suis sur la bonne voie, mais je ne peux pas obtenir ce correctement l'analyse:analyse syntaxique JSON avec GSON Structure
(très simplifié) JSON sortie:
{
"data":{
"info":{
"username": "something"
"email" : "something"
}
..
..
}
Je suis en train d'obtenir "nom d'utilisateur" et « e-mail en utilisant les catégories suivantes:
class ProfileResponse {
static Data data;
public static Data getData() {
return data;
}
public static void setData(Data data) {
ProfileResponse.data = data;
}
}
Class Data {
@SerializedName("info")
static Info info;
public static Info getInfo() {
return info;
}
public static void setInfo(Info info) {
Data.info = info;
}
}
class Info {
@SerializedName("username")
static String username;
@SerializedName("email")
static String email;
public static String getUsername() {
return username;
}
public static String getEmail() {
return email;
}
}
et désérialisation la chaîne JSON (pourrait-il être un problème que c'est une chaîne?) Comme ceci:
Gson gson = new Gson();
gson.fromJson(response, ProfileResponse.class);
if (Info.getUsername() == null
|| Info.getUsername().isEmpty()) {
System.out.println("NO USERNAME");
} else {
System.out.println("USERNAME: "
+ Info.getUsername());
}
Ceci est l'impression « NO USERNAME "chaque fois qu'il est exécuté.