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 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;
}
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;
}
thanks a Lot sir... this helped me a lot especially those urls are fine thank a lot...
ReplyDeleteInteresting approach about Generic JSON parser in android..Keep sharingAndroid Training in chennai
ReplyDeleteA Good Example to understand the subject easily.
ReplyDeleteCheck here for latest updates in Android
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
ReplyDeleteGood 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.
ReplyDeletefull stack developer training in annanagar
full stack developer training in tambaram
full stack developer training in velachery
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.
ReplyDeleteselenium training in pune
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.
ReplyDeletepython training institute in chennai
python training in Bangalore
python training institute in chennai
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.
ReplyDeleteData Science training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
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.
ReplyDeletejava training in chennai
java training in marathahalli | java training in btm layout
I have been meaning to write something like this on my website and you have given me an idea. Cheers.
ReplyDeleteangularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs online Training
angularjs Training in marathahalli
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.
ReplyDeleteSelenium 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
Feel happy to read the post
ReplyDeletesoftware testing training in chennai
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.
ReplyDeleteMicrosoft Azure online training
Selenium online training
Java online training
Java Script online training
Share Point online training
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.
ReplyDeleteData warehouse training chennai | Data warehousing training chennai
Wonderful Blog. the blog is really admired and easily can able to understand the Concept.
ReplyDeleteData Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
"Very good post!!! Thanks for sharing with us. It is more useful for us...
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
"
Great Article. Thank you for sharing! Really an awesome post for every one.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
I love this code and blog..good sharing!!!
ReplyDeleteAndroid Training in Chennai | Certification | Mobile App Development Training Online | Android Training in Bangalore | Certification | Mobile App Development Training Online | Android Training in Hyderabad | Certification | Mobile App Development Training Online | Android Training in Coimbatore | Certification | Mobile App Development Training Online | Android Training in Online | Certification | Mobile App Development Training Online
This is one amazing piece of article. Helped a lot in increasing my knowledge.
ReplyDeleteDigital Marketing Training in Chennai | Certification | SEO Training Course | Digital Marketing Training in Bangalore | Certification | SEO Training Course | Digital Marketing Training in Hyderabad | Certification | SEO Training Course | Digital Marketing Training in Coimbatore | Certification | SEO Training Course | Digital Marketing Online Training | Certification | SEO Online Training Course
ReplyDeleteThis 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
Wow, amazing weblog format! How lengthy have you been running a blog for? you make running a blog look easy. The total glance of your website is wonderful, let alone the content!
ReplyDeletedata science training in chennai
data science training in velachery
android training in chennai
android training in velachery
devops training in chennai
devops training in velachery
artificial intelligence training in chennai
artificial intelligence training in velachery
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
ReplyDeletedata scientist training and placement
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.
ReplyDeleteBest Data Science Courses
Great post. keep sharing such a worthy information.
ReplyDeleteRPA Training in Chennai
RPA Training Online
RPA Training In Bangalore
Mindblowing blog very useful thanks
ReplyDeleteSEO Training in T Nagara
SEO Training in Chennai
Yeni Perde Modelleri
ReplyDeletesms onay
Vodafone mobil ödeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
trafik sigortası
dedektör
web sitesi kurma
Ask Romanlari
smm panel
ReplyDeleteSmm Panel
https://isilanlariblog.com
instagram takipçi satın al
hirdavatciburada.com
beyazesyateknikservisi.com.tr
servis
JETON HİLE
ataşehir vestel klima servisi
ReplyDeleteataşehir daikin klima servisi
kartal daikin klima servisi
ümraniye daikin klima servisi
beykoz toshiba klima servisi
üsküdar toshiba klima servisi
beykoz beko klima servisi
üsküdar beko klima servisi
ümraniye vestel klima servisi
yurtdışı kargo
ReplyDeleteözel ambulans
uc satın al
en son çıkan perde modelleri
en son çıkan perde modelleri
minecraft premium
nft nasıl alınır
lisans satın al