Using Jackson to manipulate JSON

Source: Internet
Author: User
Tags openid

1. Introduction of Jasckson-core.jar,jackson-annotations.jar and Jackson-databind.jar

2. Because no JSON data is fetched and submitted from HTTP, do a data class instead

1 classWxjson {2      PublicString Getaccess_token () {3         returnAccess_token;4     }5 6      Public voidSetaccess_token (String access_token) {7          This. Access_token =Access_token;8     }9 Ten      Public intgetexpires_in () { One         returnexpires_in; A     } -  -      Public voidSetexpires_in (intexpires_in) { the          This. expires_in =expires_in; -     } -  -      PublicString Getrefresh_token () { +         returnRefresh_token; -     } +  A      Public voidSetrefresh_token (String refresh_token) { at          This. Refresh_token =Refresh_token; -     } -  -      PublicString Getopenid () { -         returnOpenID; -     } in  -      Public voidSetopenid (String OpenID) { to          This. OpenID =OpenID; +     } -  the      PublicString Getscope () { *         returnscope; $     }Panax Notoginseng  -      Public voidsetscope (String scope) { the          This. Scope =scope; +     } A  the      PublicString Getunionid () { +         returnUnionid; -     } $  $      Public voidSetunionid (String unionid) { -          This. Unionid =Unionid; -     } the  -     PrivateString Access_token;Wuyi     Private intexpires_in; the     PrivateString Refresh_token; -     PrivateString OpenID; Wu     PrivateString scope; -     PrivateString Unionid; About}

3 The following first converts the data into JSON data in two ways, and then converts the converted JSON data back to the list object

1 ImportCom.fasterxml.jackson.databind.ObjectMapper;2 3 Importjava.io.IOException;4 Importjava.util.ArrayList;5 Importjava.util.List;6 7  Public classMain {8 9      Public Static voidMain (string[] args)throwsIOException {Ten  OneWxjson Wxjson =NewWxjson (); AObjectmapper mapper =NewObjectmapper (); -  -         //Convert a Java object to JSON theWxjson.setaccess_token ("Access_token"); -Wxjson.setexpires_in (7200); -Wxjson.setrefresh_token ("Refresh_token"); -Wxjson.setopenid ("OPENID"); +Wxjson.setscope ("SCOPE"); -Wxjson.setunionid ("O6_BMASDASDSAD6_2SGVT7HMZOPFL"); +String JSON =mapper.writevalueasstring (Wxjson); A System.out.println (JSON); at  -List<wxjson> wxjsons =NewArraylist<wxjson>(); - Wxjsons.add (Wxjson); -String jsonlist =mapper.writevalueasstring (wxjsons); - System.out.println (jsonlist); -  in  -Wxjson WxJson1 = Mapper.readvalue (JSON, Wxjson.class); toSystem.out.println (Wxjson1.getaccess_token () + "" +wxjson1.getexpires_in ()); +  -     } the}

Output results

1 {"Access_token": "Access_token", "expires_in": 7200, "Refresh_token": "Refresh_token", "OpenID": "OpenID", " Scope ":" Scope "," Unionid ":" O6_BMASDASDSAD6_2SGVT7HMZOPFL "}2 [{" Access_token ":" Access_token "," Expires_in ": 7200," Refresh_token ":" Refresh_token "," OpenID ":" OpenID "," Scope ":" Scope "," Unionid ":" O6_bmasdasdsad6_ 2SGVT7HMZOPFL "}]3 access_token 7200

Using Jackson to manipulate JSON

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.