-1

I am doing a request which gives me the following answer :

{
  "SuL":[
    {"IdS":"63","Nam":"Quiz1","Cat":"4097"},
    {"IdS":"64","Nam":"6e","Cat":"4099"},
    {"IdS":"65","Nam":"CP","Cat":"4100"},
    {"IdS":"66","Nam":"CE1","Cat":"4098"}
  ],    
  "CaL":[
    {"Cod":"4097","Lab":"Categorie1"},
    {"Cod":"4098","Lab":"Math"},
    {"Cod":"4099","Lab":"Anglais"},
    {"Cod":"4100","Lab":"Géographie"}
  ]
}

What I am trying to do is to place all the "Lab" values in a List of string. SuL represent a list of available objects on a server (which im not interested in right now) and CaL represents categories.

Here is my Categorie class :

public class Categorie {

  public String Cod;
  public String Lab;

  public String getCode() {
    return Cod;
  }
  public void setCode(String code) {
    this.Cod = code;
  }
  public String getName() {
    return Lab;
  }
  public void setName(String name) {
    this.Lab = name;
  }
}

And here is my AsyncTask doing the request and the Gson parsing :

class DownloadQuizCategory extends AsyncTask<String, String, String> {

  protected String doInBackground(String...uri) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;
    String responseString = null;
    ByteArrayOutputStream out;
    try {
      response = httpclient.execute(new HttpGet(uri[0]));
      StatusLine statusLine = response.getStatusLine();
      if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
        out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);
        out.close();
        responseString = out.toString();
      } else {
        // Closes the connection.
        response.getEntity().getContent().close();
        throw new IOException(statusLine.getReasonPhrase());
      }
    } catch (ClientProtocolException e) {
      // TODO Handle problems..
    } catch (IOException e) {
      // TODO Handle problems..
    }
    Log.i("AsyncTask", "responseString : " + responseString);
    return responseString;
  }

  @Override
  protected void onPostExecute(String resultat) {
    super.onPostExecute(resultat);
    //PARSING HERE :(
  }
}

This is called by :

new DownloadQuizCategory().execute(URL_category); //where my results are
MikO
  • 18,243
  • 12
  • 77
  • 109
P.h. Dvrgne
  • 131
  • 1
  • 1
  • 7
  • Can you rephrase your question telling us what is the output you get with this code and what you expect to obtain? – mariosangiorgio Jun 27 '13 at 15:50
  • I answer another question about parsing in here, check it out. http://stackoverflow.com/questions/17294844/how-can-i-parse-without-any-array-just-a-single-object/17295405#17295405 – jpardogo Jun 27 '13 at 16:32

2 Answers2

1

You only need to create another class apart from your Categorie class. Something like this:

public class Response {
    private List<Categorie> CaL;
    //getter & setter
}

Then parse your response with:

Gson gson = new Gson();
Response response = gson.fromJson(resultat, Response.class);

Gson will automatically and silently skip all the values into "SuL", since there's no attribute called SuL into your Response class...


EDIT: Then, if you want all the "Lab" vlaues into a List<String>, you just need to do (this is Java basics...):

List<String> labs = new ArrayList<String>();
for (Categorie c : response.getCaL()) {
   labs.add(c.getName());
}
MikO
  • 18,243
  • 12
  • 77
  • 109
0

You have to deserialize your Json, try this lines above. Your JSONArray Lab should contain your list of "Lab".

@Override
protected void onPostExecute(String resultat)
{
    super.onPostExecute(resultat);
    JSONObject Response = new JSONObject(resultat);
    JSONArray Lab= (JSONArray) Response.get("Lab");
    System.out.print(Lab);
}
Adrien Cerdan
  • 1,005
  • 1
  • 11
  • 21