Tuesday, August 28, 2012

JSON GSON

JSON is a wide spread data description format used to exchange JavaScript Objects.
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 :



13 comments:

  1. 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.

    ReplyDelete
  2. 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.
    rpa training in bangalore
    best rpa training in bangalore
    RPA training in bangalore
    rpa course in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  3. 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.
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  4. 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.
    AWS 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

    ReplyDelete
  5. 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.
    AWS Training in Bangalore
    AWS training in sholinganallur
    AWS training in Tambaram
    AWS training in Velachery

    ReplyDelete
  6. 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.

    ReplyDelete
  7. I 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…

    ReplyDelete
  8. Good 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

    ReplyDelete