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 :
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
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.
ReplyDeletenice blog
ReplyDeletedevops training in bangalore
hadoop training in bangalore
iot training in bangalore
machine learning training in bangalore
uipath training in bangalore
nice 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
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
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
It 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
College issue above. Big fact marriage choose many statement read answer. Like magazine former attention.news today live
ReplyDelete