Parse JSON using gson

Source: Internet
Author: User

The previous blog said that the method for parsing JSON into Java is

These jar packages, however, encountered a problem at the parsing layer, that is, an exception will be thrown when the time string is converted to the Java timestamp object. This problem has not been solved, two days ago, I suddenly thought about it and went online to find some solutions. After a long time, I didn't have a perfect solution, either looking complicated or failing to implement it, finally, I found that it was quite convenient to use Google's gson to parse JSON, so I tried to use gson to parse JSON. It was quite helpful and the problem was quickly solved.

Here is a simple example.

This is a Java Bean

 

Package Baz. javaBean; import Java. SQL. timestamp; public class product {private string name; private int ID; private timestamp date; Public Product () {// todo auto-generated constructor stub} public product (string name, int ID, timestamp date) {super (); this. name = Name; this. id = ID; this. date = date;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public int GETID () {return ID;} public void setid (int id) {This. id = ID;} public timestamp getdate () {return date;} public void setdate (timestamp date) {This. date = date ;}}

 


Below is a test class

 

Package Baz. test; import Baz. javaBean. product; import COM. google. gson. gson; public class test1 {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubgson gson = new gson (); // a new gson object // JSON string JSON = "{\" Name \": \ "guolicheng \", \ "ID \": 123456, \ "date \": \ "12:36:54 \"}"; // new a product object Product = new product (); // converts a JSON string to a Java object Product = gson. fromjson (JSON, product. class); // output system. out. println ("name:" + product. getname (); system. out. println ("ID:" + product. GETID (); system. out. println ("Date:" + product. getdate ());}}

Output result:

 

Name: guolicheng
ID: 123456
Date: 2013-04-13 12:36:54. 0


It's easy.

To use the jar package, can you download the http://pan.baidu.com/share/link here? Consumer id = 440391 & UK = 3457081238

Or download it from the official website.

For more in-depth introduction, please read these blogs.

Gson use http://www.360doc.com/content/11/0309/16/1332348_99601000.shtml

Gson Study Notes http://www.360doc.com/content/11/0111/14/1332348_85735384.shtml

JSON data parsing http://www.360doc.com/content/12/0903/10/7662927_233881217.shtml

Gson uses an example http://www.360doc.com/content/11/0815/14/7322578_140545909.shtml in Android

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.