Json object android retrofit

Résolu
DalyMHY Messages postés 6 Date d'inscription   Statut Membre Dernière intervention   -  
DalyMHY Messages postés 6 Date d'inscription   Statut Membre Dernière intervention   -
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"}}

2 réponses

  1. BunoCS Messages postés 436 Date d'inscription   Statut Modérateur Dernière intervention   3 930
     
    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
    1. DalyMHY Messages postés 6 Date d'inscription   Statut Membre Dernière intervention  
       
      Merci pour ton aide.
      J'ai réussi à trouver la solution
       
      JSONObject jsonData = jsonObject.getJSONObject("data");
      Integer id = jsonData.getInt("id");
      0
  2. BunoCS Messages postés 436 Date d'inscription   Statut Modérateur Dernière intervention   3 930
     
    Hello,

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

    0
    1. DalyMHY Messages postés 6 Date d'inscription   Statut Membre Dernière intervention  
       
      J'ai pas réussi à trouver la résultat
      0