Json object android retrofit

Résolu/Fermé
DalyMHY Messages postés 6 Date d'inscription mercredi 18 mars 2020 Statut Membre Dernière intervention 19 mars 2020 - Modifié le 19 mars 2020 à 10:24
DalyMHY Messages postés 6 Date d'inscription mercredi 18 mars 2020 Statut Membre Dernière intervention 19 mars 2020 - 19 mars 2020 à 12:24
Bonjour,
J'ai cette fonction qui retourne une réponse Json détaillée, je juste avoir l'id comment faire s'il te plait ?
 
public void onResponse(Call<String> call, Response<String> response) {

  if (response.isSuccessful()) {
    if (response.body() != null) {
      Log.i("Responsestring", response.body().toString());
      Log.i("onSuccess", response.body().toString());
      String jsonresponse = response.body().toString();
      try {
        JSONObject jsonObject = new JSONObject(jsonresponse);
        if (jsonObject.getString("status").equals("success")) {
          Toast.makeText(getActivity(), "Login Successfully!", Toast.LENGTH_SHORT).show();
           Intent intent;
           intent = new Intent(getActivity(), ActivityListEvents.class);
           startActivity(intent);
           Log.i("DATA", jsonObject.getString("data"));
        }



I/Responsestring: {"status":"success","data":{"id":2,"name":"user","prenom":"user"}}
I/onSuccess: {"status":"success","data{"id":2,"name":"user","prenom":"user"}}
I/DATA: {"id":2,"name":"user","prenom":"use"}}
A voir également:

2 réponses

BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 3 895
19 mars 2020 à 12:18
Tu n'as pas dû chercher beaucoup ;)
Aller, c'est bien parce que c'est rapide à faire...A priori, il te suffit de faire ceci :
  if (response.isSuccessful() && response.body() != null) {
      String jsonresponse = response.body().toString();
      try {
        JSONObject jsonObject = new JSONObject(jsonresponse);
        if (jsonObject.getString("status").equals("success")) {
          JSONObject jsonData = jsonObject.getString("data"));
          int id = jsonData.getInt("id");
        }
 ...

1
DalyMHY Messages postés 6 Date d'inscription mercredi 18 mars 2020 Statut Membre Dernière intervention 19 mars 2020
19 mars 2020 à 12:24
Merci pour ton aide.
J'ai réussi à trouver la solution
 
JSONObject jsonData = jsonObject.getJSONObject("data");
Integer id = jsonData.getInt("id");
0
BunoCS Messages postés 15475 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 23 avril 2024 3 895
Modifié le 19 mars 2020 à 10:27
Hello,

Tu peux parcourir ton JSONObject avec les méthodes
get()
,
getJSONObject()
,
getString()
.
Plus d'infos ici

0
DalyMHY Messages postés 6 Date d'inscription mercredi 18 mars 2020 Statut Membre Dernière intervention 19 mars 2020
19 mars 2020 à 11:06
J'ai pas réussi à trouver la résultat
0