Simple use of the Convert JSON tool--jackson

Source: Internet
Author: User

  What is Jackson?

Conversion of Java objects to JSON strings can be easily implemented

Preparatory work: Guide Package

Jackson's Jar All:http://jackson.codehaus.org/1.7.6/jackson-all-1.7.6.jar

 1. The entity object goes to JSON  

 Jackson uses Getter method to locate attributes (not fields)
You can omit some getter by adding a callout @JsonIgnore

To convert an object or collection to a JSON string

 PackageCn.jackson;Importjava.io.IOException;Importjava.util.Arrays;Importjava.util.List;Importorg.codehaus.jackson.JsonGenerationException;ImportOrg.codehaus.jackson.annotate.JsonIgnore;Importorg.codehaus.jackson.map.JsonMappingException;ImportOrg.codehaus.jackson.map.ObjectMapper; Public classCustomer {PrivateString name; PrivateString ID;  PublicCustomer () {Super(); }     PublicCustomer (string name, string id) {Super();  This. Name =name;  This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString getId () {returnID; }     Public voidsetId (String id) { This. ID =ID; } @JsonIgnore PublicString getcity () {returnBeijing; }     PublicString Getbirth () {return"1988"; }         Public Static voidMain (string[] args)throwsjsongenerationexception, Jsonmappingexception, IOException {//Guide Package//Create a Objectmapper objectObjectmapper mapper =NewObjectmapper (); //Customer cust =NewCustomer ("Jackson", "1001"); String Jsonstr=mapper.writevalueasstring (Cust);                System.out.println (JSONSTR); List<Customer> list = (list) arrays.aslist (Cust,NewCustomer ("33", "off-soul meter")); String jsonstrlist=mapper.writevalueasstring (list);        System.out.println (jsonstrlist); /*** Jackson uses Getter method to position properties * can be ignored by adding annotations @JsonIgnore to make some getter*/    }}

  Results:

{"Name": "Jackson", "id": "1001", "Birth": "1988"}[{"name": "Jackson", "id": "1001", "Birth": "1988"},{"name": " "," "id": "Off-soul meter", "Birth": "1988"}]

 So ajax_day02 can switch to using Jackson to simplify:

    Objectmapper mapper=New  objectmapper ();        String result=mapper.writevalueasstring (SC);

  2.JSON string to object

New Objectmapper ();         = "{\" name\ ": \" jackson\ ", \" id\ ": \" 1001\ "}";         = Mapper.readvalue (JSON, Customer.  Class);        System.out.println (C.getid ());

 Note "The Escape operation

Simple use of the Convert JSON tool--jackson

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.