dans les dernières heures je travaillais avec REST et Gson. Mais maintenant je suis coincé.Gson obtenir des valeurs de tableau complexes
Je possède ce code:
public class Main {
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
public static void main(String[] args) throws IOException {
Main example = new Main();
String response = example.run("http://localhost/crud/api.php/contacts/");
System.out.println(response);
Gson gson = new Gson();
Contact fetchedContacts = gson.fromJson(response, Contact.class);
System.out.println(fetchedContacts.toString());
}
}
Et mon serveur apache localhost me retourner ce JSON
{
"contacts":
{
"columns":
[
"id",
"name",
"email",
"tel"
],
"records":
[
[
1,
"Simon",
"[email protected]",
"911"
],
[
2,
"Ganter",
"[email protected]",
"911"
]
]
}
}
Mais l'objet est null
. Je maintenant ce code ne fonctionne pas. Mais comment le réparer? Quelles sont les prochaines étapes à suivre? J'ai aussi une classe pour mes Contacs:
public class Contact {
private String name;
private String email;
private String tel;
public Contact(String name, String email, String tel) {
this.name = name;
this.email = email;
this.tel = tel;
}
les besoins du serveur Apache fixe si c'est votre modèle Gson vous voulez ... –
Will not vous avez besoin la propriété id? –