It is pretty widely used for web API.
Today, I will focus on the exploitation of this kind of data in Java, with the pretty well done GSon library.
First, call the API (a simple GET with the arguments in the URL) that provide the data, this is done simply by this code :
public static String call(String url) throws IOException { BufferedReader bis = null; InputStream is = null; try { URLConnection connection = new URL(url).openConnection(); is = connection.getInputStream(); // warning of UTF-8 data bis = new BufferedReader(new InputStreamReader(is, "UTF-8")); String line = null; StringBuffer result = new StringBuffer(); while ((line = bis.readLine()) != null) { result.append(line); } return result.toString(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Note : we use a InputStreamReader(is, "UTF-8") in order to manage correctly UTF-8 data that we'll retrieve in some Japanese, French or other exotic languages.
Ok, the String result will be the something, for example like that :
{
"first-name": "John",
"last-name": "Smith",
"age": 25,
"address":
{
"street-address": "21 2nd Street",
"city": "New York",
"state": "NY",
"postal-code": "10021"
},
"phone-number":
[
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
To process this String, we'll use the magic GSon.
The GSon library will try to parse the JSon formatted string and map the data to a given Java Person Object. GSon used reflection to do such a trick, so the attributes types and names of the target java Object will need to be matchable with the JSon string structure.
In the previous example, the Person object will need to have a structure that match the JSon object, that is to say, will have 5 attributes named "first-name", "last-name", "age", "address", and "phone-number".
First of all, what we see here, is that an attribute cannot be called first-name in Java, because the '-' character is not permitted in java attribute names. No problem, we'll declare a firstName attribute, and specify that it's JSon serialized name id "first-name" by using a @SerializedName annotation.
We'll obtain an attribute declared like this :
@SerializedName("first-name") private String firstName;
Of course same trick is applied for the "last-name" attribute.
The age is simply declared as a int, here we keep the same attribute name in JavaScript and in Java :
private int age;
Ok, now the address, which is a complex Object (we can see it with the {...} that represents a structure.
So we'll map it with a Java Object Address, which following the same principle as above, which give us :
private class Address { @SerializedName("street-address") private String streetAddress; private String city; private String state; @SerializedName("postal-code") private int postalCode; }
Remain the phone numbers, here the [...] characters point out that we are dealing with a collections of objects.
We'll then map it with a java.util.List of PhoneNumber Objects as below :
@SerializedName("phone-number") private List<PhoneNumber> phoneNumber;
Where a PhoneNumber is the given object :
private class PhoneNumber { private String type; private String number; }
OK, so now our Object Person is completed and looks like that :
private class Person { @SerializedName("first-name") private String firstName; @SerializedName("last-name") private String lastName; private int age; private Address address; @SerializedName("phone-number") private List<PhoneNumber> phoneNumber; }
We are done. A call to Gson will instantiate a Person Java Object and populate it with the flux.
Person data = new Gson().fromJson(json, Person .class);
That give us :
Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java Online Training from India. or learn thru Java EE Online Training Students.
ReplyDeleteGreat Article android based projects
DeleteJava Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai
Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
ReplyDeleterpa training in bangalore
best rpa training in bangalore
RPA training in bangalore
rpa course in bangalore
rpa training in chennai
rpa online training
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.
ReplyDeletepython Course in Pune
python Course institute in Chennai
python Training institute in Bangalore
Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
ReplyDeleteAWS Course Interview Questions and Answers 2019 | AWS Interviews Questions and Answers for Devops
AWS Tutorial 2019
AWS Interview questions and answers |AWS Interview Question and Answers 2019
AWS Tutorial for Beginners 2019 | AWS Tutorial with AWS Training Videos
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.
ReplyDeleteAWS Training in Bangalore
AWS training in sholinganallur
AWS training in Tambaram
AWS training in Velachery
Attend The Python Training in Bangalore From ExcelR. Practical Python Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python Training in Bangalore.
ReplyDeleteI have express a few of the articles on your website now, and I really like your style of Python training in pune blogging. I added it to my favorite’s blog site list and will be checking back soon…
ReplyDeletenice blog
ReplyDeletedevops training in bangalore
hadoop training in bangalore
iot training in bangalore
machine learning training in bangalore
uipath training in bangalore
VISIT HERE ==> BIG DATA AND HADOOP TRAINING IN BANGALORE
ReplyDeletenice blog
ReplyDeleteVisit here - Devops training in Bangalore
Visit here - Hadoop Training in Bangalore
ReplyDeleteGood post, Thank you so much for sharing this lovely article. It was so good to read and useful to upgrade my understanding. Keep it up!! machine learning course bangalore
ReplyDeleteFor Devops training in Bangalore Visit:
ReplyDeleteDevops Training in Bangalore
Attend The Machine Learning course Bangalore From ExcelR. Practical Machine Learning course Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Machine Learning course Bangalore.
ReplyDeleteMachine Learning course Bangalore
Really a very nice blog i really appreciate all your efforts ,thank you so much for sharing.
ReplyDeleteWeb Designing Training in Chennai
Web Designing Course in Chennai
Web Designing Training in Bangalore
Web Designing Course in Bangalore
Web Designing Training in Hyderabad
Web Designing Course in Hyderabad
Web Designing Training in Coimbatore
Web Designing Training
Web Designing Online Training
I think this is a really good article. You make this information interesting and engaging. ExcelR Machine Learning Courses You give readers a lot to think about and I appreciate that kind of writing.
ReplyDeleteAttend The Machine Learning Course Bangalore From ExcelR. Practical Machine Learning course Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Machine Learning course Bangalore.
ReplyDeleteMachine Learning Course Bangalore
the content on your blog was really helpful and informative. Thakyou. # BOOST Your GOOGLE RANKING.It’s Your Time To Be On #1st Page
ReplyDeleteOur Motive is not just to create links but to get them indexed as will
Increase Domain Authority (DA).We’re on a mission to increase DA PA of your domain
High Quality Backlink Building Service
1000 Backlink at cheapest
50 High Quality Backlinks for just 50 INR
2000 Backlink at cheapest
5000 Backlink at cheapest
the content on your blog was really helpful and informative. Thakyou. # BOOST Your GOOGLE RANKING.It’s Your Time To Be On #1st Page
ReplyDeleteOur Motive is not just to create links but to get them indexed as will
Increase Domain Authority (DA).We’re on a mission to increase DA PA of your domain
High Quality Backlink Building Service
1000 Backlink at cheapest
50 High Quality Backlinks for just 50 INR
2000 Backlink at cheapest
5000 Backlink at cheapest
Annabelle loves to write and has been doing so for many years.Backlink Indexer My GPL Store Teckum-All about Knowledge
ReplyDeleteAnnabelle loves to write and has been doing so for many years.Backlink Indexer My GPL Store Teckum-All about Knowledge
ReplyDeleteIt was wonderfull reading your article. Great writing style # BOOST Your GOOGLE RANKING.It’s Your Time To Be On #1st Page Our Motive is not just to create links but to get them indexed as will Increase Domain Authority (DA).We’re on a mission to increase DA PA of your domain High Quality Backlink Building Service 1000 Backlink at cheapest 50 High Quality Backlinks for just 50 INR 2000 Backlink at cheapest Captured Current News
ReplyDeleteI recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end.
ReplyDeletePython Training In Pune