×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Hafiz Waleed Hussain
Added: Jul 4, 2013 2:33 AM
Modified: Jul 4, 2013 2:40 AM
Views: 153
Tags: android
Now here i am giving the code of Generic Parsor which you can use to any type of JSON to parse. And if you feel any difficulty you can check my blog for this code snippet.
http://uwanttolearnandroid.blogspot.com/p/generic-json-parser-in-android.html
  1.  public ArrayList jsonParser(JSONArray jsonArray, Class myClass,
  2.    ArrayList data) {
  3.  
  4.   Gson g = new Gson();
  5.   JSONArray ja = jsonArray;
  6.   try {
  7.  
  8.    for (int i = 0; i < ja.length(); i++) {
  9.     showLog(ja.getString(i), myClass);
  10.     data.add(g.fromJson(ja.getString(i), myClass));
  11.     // listener.loading(i,ja.length());
  12.    }
  13.   } catch (JSONException e) {
  14.    // TODO Auto-generated catch block
  15.    e.printStackTrace();
  16.   }
  17.  
  18.   return data;
  19.  }
  20.  
  21.  public ArrayList jsonParser(InputStream in, Class myClass, ArrayList data) {
  22.   String line;
  23.   Gson g = new Gson();
  24.   try {
  25.    while ((line = br.readLine()) != null) {
  26.     JSONArray ja = new JSONArray(line);
  27.     for (int i = 0; i < ja.length(); i++) {
  28.      showLog(ja.getString(i), myClass);
  29.      data.add(g.fromJson(ja.getString(i), myClass));
  30.      // listener.loading(i,ja.length());
  31.     }
  32.    }
  33.   } catch (JsonSyntaxException e) {
  34.    // TODO Auto-generated catch block
  35.    e.printStackTrace();
  36.   } catch (IOException e) {
  37.    // TODO Auto-generated catch block
  38.    e.printStackTrace();
  39.   } catch (JSONException e) {
  40.    // TODO Auto-generated catch block
  41.    e.printStackTrace();
  42.   } catch (Exception e) {
  43.    // TODO Auto-generated catch block
  44.    e.printStackTrace();
  45.   }
  46.   return data;
  47.  }
  48.