Generic JSON parser in android

Hi All,
Now suppose we have a JSON like below image.


Now first we are making model which save our data when we load from Server.
Now for time saving you can create model of your JSON using these two websites.
1.http://jsongen.byingtondesign.com/
2.http://www.jsonschema2pojo.org/

Now my model is looking like.
public class Model {

private String id;
private String category_id;
private String name;
private String latitude;
private String longitude;
private String description;
private String website;
private List<String> images = new ArrayList<String>();

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getCategory_id() {
return category_id;
}

public void setCategory_id(String category_id) {
this.category_id = category_id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLatitude() {
return latitude;
}

public void setLatitude(String latitude) {
this.latitude = latitude;
}

public String getLongitude() {
return longitude;
}

public void setLongitude(String longitude) {
this.longitude = longitude;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getWebsite() {
return website;
}

public void setWebsite(String website) {
this.website = website;
}

public List<String> getImages() {
return images;
}

public void setImages(List<String> images) {
this.images = images;
}

}

Now final steps to parse any JSON. But for example we use above JSON.
Here you also need gson library. So download gson and build path with your project.

Now after http request i get the JSONArray in response object.
Now first i am calling method jsonParser who give me ArrayList<Model> after parsing. 


ArrayList<Model> data = new ArrayList<Model>();
       jsonParser(response,Model.class,data);

Now use data.get(0).getName();
Simple.
Complete

For Inputstream:
Same use overload method and give him the InputStream rather then JSONArray.
The code of  jsonParser(...) method write at the end of the blog page.
Now we use more examples so we prove this is a generic parser.

Now we have JSON like.

   "firstName":"John" , 
   "lastName":"Doe" 
}, 

   "firstName":"Anna" ,
   "lastName":"Smith" }, 

   "firstName":"Peter" ,
   "lastName":"Jones" 
}
 Now create Model.
public class PersonModel{

   private String firstName;
   private String lastName;
   // Getter and setters
   .....
}

ArrayList<PersonModel> data = new ArrayList<PersonModel>();
jsonParse(response,PersonModel.class,data);
finish work. :)


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public ArrayList jsonParser(JSONArray jsonArray, Class myClass,
ArrayList data) {

Gson g = new Gson();
JSONArray ja = jsonArray;
try {

for (int i = 0; i < ja.length(); i++) {
showLog(ja.getString(i), myClass);
data.add(g.fromJson(ja.getString(i), myClass));
// listener.loading(i,ja.length());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return data;
}

public ArrayList jsonParser(InputStream in, Class myClass, ArrayList data) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
Gson g = new Gson();
try {
while ((line = br.readLine()) != null) {
JSONArray ja = new JSONArray(line);
for (int i = 0; i < ja.length(); i++) {
showLog(ja.getString(i), myClass);
data.add(g.fromJson(ja.getString(i), myClass));
// listener.loading(i,ja.length());
}
}
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}

29 comments:

  1. thanks a Lot sir... this helped me a lot especially those urls are fine thank a lot...

    ReplyDelete
  2. Interesting approach about Generic JSON parser in android..Keep sharingAndroid Training in chennai

    ReplyDelete
  3. A Good Example to understand the subject easily.

    Check here for latest updates in Android

    ReplyDelete
  4. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in android programming your post help me a lot.Thanks for your informative article. Best Android Training in chennai |Android Training in chennai

    ReplyDelete
  5. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    full stack developer training in annanagar

    full stack developer training in tambaram

    full stack developer training in velachery

    ReplyDelete
  6. I found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article.

    selenium training in pune

    ReplyDelete
  7. From your discussion I have understood that which will be better for me and which is easy to use. Really, I have liked your brilliant discussion. I will comThis is great helping material for every one visitor. You have done a great responsible person. i want to say thanks owner of this blog.
    python training institute in chennai
    python training in Bangalore
    python training institute in chennai

    ReplyDelete
  8. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.

    Data Science training in Chennai
    Data science training in bangalore
    Data science online training
    Data science training in pune

    ReplyDelete
  9. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    java training in chennai

    java training in marathahalli | java training in btm layout

    ReplyDelete
  10. I feel happy to find your post, excellent way of writing and also I would like to share with my colleagues so that they also get the opportunity to read such an informative blog.
    Selenium Training in Chennai
    selenium Classes in chennai
    iOS Training in Chennai
    Digital Marketing Training in Chennai
    Salesforce Training institutes in adyar
    Salesforce Training institutes in Tambaram

    ReplyDelete
  11. Thanks for such a great article here. I was searching for something like this for quite a long time and at last I’ve found it on your blog. It was definitely interesting for me to read  about their market situation nowadays.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training

    ReplyDelete
  12. Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.

    Data warehouse training chennai | Data warehousing training chennai







    ReplyDelete

  13. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points. To appreciate this I like to share some useful information regarding Microsoft Azure which is latest and newest,
    Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  14. Magnificent beat ! I wish to apprentice while you amend your site, how could i subscribe for a blog web site? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast offered bright clear idea
    data scientist training and placement

    ReplyDelete
  15. Nice blog and informative content. Keep sharing more stuff like this. Thank you. If you want data science course training, check out the below link.
    Best Data Science Courses

    ReplyDelete