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