Example of parsing JSON using Jackson framework

Source: Internet
Author: User

First you need to download package: jackson-databind-2.0.5.jarjackson-annotations-2.0.5.jarjackson-core-2.0.5.jar, URL in: http://wiki.fasterxml.com/JacksonDownload

 

Custom. JSON:

{
"Country": "USA ",
"Countryinfo": {"golden": 40, "Silver": 23, "copper": 33, "tank": 1 },
"Countrydetail ":[
{"ID": "no", "Header": "Number", "fieldindex": "no", "sortorder": NULL, "printable": true },
{"ID": "name", "Header": "name", "fieldindex": "name", "sortorder": NULL, "printable": true },
{"ID": "Total", "Header": "total score", "fieldindex": "Total", "sortorder": NULL, "printable": true },
{"ID": "detail", "Header": "details", "fieldindex": "detail", "sortorder": NULL, "printable": true}
],

"Orgood": true}

Java Bean and parsing:

Import java. Io. file;
Import java. Io. ioexception;
Import com. fasterxml. Jackson. Core. jsonparseexception;
Import com. fasterxml. Jackson. databind. jsonmappingexception;
Import com. fasterxml. Jackson. databind. objectmapper;

Public class bean {

Private string country;
Private Boolean orgood;
Private country countryinfo;
Private countrydetail [] countrydetail;

Public String getcountry (){
Return country;
}

Public void setcountry (string country ){
This. Country = Country;
}

Public Boolean isorgood (){
Return orgood;
}

Public void setorgood (Boolean orgood ){
This. orgood = orgood;
}

Public Country getcountryinfo (){
Return countryinfo;
}

Public void setcountryinfo (country countryinfo ){
This. countryinfo = countryinfo;
}

Public countrydetail [] getcountrydetail (){
Return countrydetail;
}

Public void setcountrydetail (countrydetail [] countrydetail ){
This. countrydetail = countrydetail;
}

Public static class country {
Public String getgolden (){
Return golden;
}
Public void setgolden (string golden ){
This. Golden = golden;
}
Public String getsilver (){
Return silver;
}
Public void setsilver (string silver ){
This. Silver = silver;
}
Public String getcopper (){
Return copper;
}
Public void setcopper (string copper ){
This. Copper = copper;
}
Public String gettank (){
Return tank;
}
Public void settank (string tank ){
This. tank = tank;
}
Private string golden;
Private string silver;
Private string copper;
Private string tank;

}

Public static class countrydetail {
Public String GETID (){
Return ID;
}
Public void setid (string ID ){
This. ID = ID;
}
Public String getheader (){
Return header;
}
Public void setheader (string header ){
This. header = header;
}
Public String getfieldindex (){
Return fieldindex;
}
Public void setfieldindex (string fieldindex ){
This. fieldindex = fieldindex;
}
Public String getsortorder (){
Return sortorder;
}
Public void setsortorder (string sortorder ){
This. sortorder = sortorder;
}
Public Boolean isprintable (){
Return printable;
}
Public void setprintable (Boolean printable ){
This. printable = printable;
}
Private string ID;
Private string header;
Private string fieldindex;
Private string sortorder;
Private Boolean printable;

}

Public static void main (string ARGs []) {
Objectmapper mapper = new objectmapper ();
Try {
Bean = Mapper. readvalue (new file ("custom. JSON"), Bean. Class );

// Read

System. Out. println ("Country:" + bean. getcountry ());
System. Out. println ("countryinfo:" + bean. getcountryinfo (). getgolden ());
System. Out. println ("countrydetail:" + bean. getcountrydetail () [1]. getheader ());
System. Out. println ("orgood:" + bean. isorgood ());
} Catch (jsonparseexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (jsonmappingexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}

}

 

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.