JSON is commonly used in the following class libraries
jackson:http://jackson.codehaus.org/
json-lib:http://json-lib.sourceforge.net/
gson:http://code.google.com/p/google-gson/
Jackson can easily convert Java objects into JSON objects and XML documents, as well as convert JSON and XML to Java objects. Not limited to Java objects, but also valid for map, List, and array.
Jsonlib's tutorials refer to previously written articles http://blog.csdn.net/xiaokui_wingfly/article/details/39890969
or other friend's article: http://www.cnblogs.com/hoojo/archive/2011/04/21/2023805.html
xstream:http://blog.csdn.net/xiaokui_wingfly/article/details/46470145
First, the preparatory work
1. Download the dependent library jar package
Jackson's jar official: Http://wiki.fasterxml.com/JacksonDownload
Then import the jar package in the project to start working
Official Example: Http://wiki.fasterxml.com/JacksonInFiveMinutes
If you need to convert XML, you also need to Stax2-api.jar
Note : The getter and setter methods are omitted from all of my examples below, please add them yourself
Example 1: A simple conversion between JSON and JavaBean
/** * Javabean/map/list/array convert JSON to/from each other * *public static void Main (string[] args) {Objectmapper objectmapper = new Objectmapper ();User user = new user ();User. Setaddress("Henan Province, China");User. Setemail("[email protected]");User. SetId(1);User. SetName("X-rapido");User. Setbirthday(New Birthday ("2015-08-06"));list<string> list = new arraylist<string> ();List. Add("a");List. Add("B");List. Add("C");map<string, object> map = new hashmap<string, object> ();Map. Put("a","1");Map. Put("B","2");Map. Put("C","3");Map. Put("List", list);String []sz = {"a","B","C"};try {System. out. println(Objectmapper. writevalueasstring(user));System. out. println(Objectmapper. writevalueasstring(list));System. out. println(Objectmapper. writevalueasstring(map));System. out. println(Objectmapper. writevalueasstring(SZ));String s ="{\" name\ ": \" x-rapido\ "}";JSON conversion JavaBean User a = Objectmapper. ReadValue(S, User. Class);System. out. println(A. GetName());JavaBean Convert JSON System. out. println(Objectmapper. writevalueasstring(a));} catch (IOException e) {E. Printstacktrace();}} Output Print: {"id":1,"Name":"X-rapido","Email":"[email protected]","Address":"Henan Province, China","Birthday":{"Birthday":"2015-08-06"}}["a","B","C"]{"B":"2","C":"3","a":"1","List":["a","B","C"]}["a","B","C"]X-rapido{"id":0,"Name":"X-rapido","Email": null,"Address": null,"Birthday": null}
For how to interact with XML and the use of Jsongenerator class, there are various write, WriteObject methods refer to the user article http://www.cnblogs.com/hoojo/archive/2011/04/22/ 2024628.html no longer wordy.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Jackson Tutorial Example