Work, need to deal with the other side of the system data exchange problems, using the method of invoking the remote interface, the data format is selected JSON, today to talk about JSON, the main analysis of JSON data and Java Bean conversion between the problem.
First, what is JSON
JSON, the full name is JavaScript object Notation, Chinese translation is the JS Objects Markup Language, is a lightweight data interchange format, easy for developers to write and read, but also easy to build and parse machine.
Second, JSON data structure
1, you can store objects (object), using curly braces {} contains the key value pair structure, key must be a string type, value is any basic type or data structure.
2. You can store arrays (array), start with brackets [], and use commas to split the elements.
3. You can store string types, number types (integer, float), Boolean (True, false), and null (you can define an object as NULL when NULL is used).
Third, grammar
1, common key value pairs.
{"Name": "Ogawa 94", "age": +, "gender": "Boy", "Hasgirlfriend": false}
2. Store objects.
{"Person": {"name": "Ogawa 94", "age": +, "gender": "Boy", "Hasgirlfriend": false}}
3. Storage array.
{"Name": "Ogawa 94", "age": +, "gender": "Boy", "Hasgirlfriend": false, "interest": ["Coding", "reading"]}
4, Object Addend group compound.
{"Person": [{"Name": "Ogawa 94_2"},{"name": "Ogawa 94_3"}]}
Iv. preparatory work
1, development system: Win7 64-bit.
2, development environment: jdk1.8.
3, development language: java.
4. Development tools: Eclipse.
5. Fastjson Jar Package: I'm using Maven's Pom.xml file configuration
<dependency> <groupId>com.alibaba</groupId> <artifactid>fastjson</ Artifactid> <version>1.2.4</version></dependency>
V. Using Ali's Fastjson toolkit to perform the actual operation of the JSON
1. Convert the JSON string to a JSON object.
1 /**2 * Convert JSON string to JSON object3 * @paramjsonstr JSON string4 * @return5 */6 Public Staticjsonobject Changestringtojson (String jsonstr) {7Jsonobject Jo =Json.parseobject (JSONSTR);8 returnJo;9}
2. Convert the JSON string array to a JSON array object.
1 /**2 * Convert a JSON string array to a JSON array object3 * @paramjsonstr JSON string4 * @return5 */6 Public StaticJsonarray Changestringtoarray (String jsonstr) {7Jsonobject Jo =Json.parseobject (JSONSTR);8 if(!jo.containskey ("person") ){9 return NULL;Ten } OneString Strarray = jo.getstring ("Person"); AJsonarray ja =Jsonarray.parsearray (strarray); - returnja; -}
3. Convert the JSON string to a Java Bean object.
1 /**2 * Convert JSON string to Java Bean Object3 * @paramjsonstr JSON string4 * @return5 */6 Public StaticPerson Changejsontobean (String jsonstr) {7Person person = json.parseobject (jsonstr, person.class);8 returnPerson ;9}
4. Convert the Java Bean object to a JSON object.
1 /**2 * Convert Java Bean object to JSON object3 * @paramPerson Java Bean Object4 * @return5 */6 Public Staticjsonobject Changebeantojson (person person) {7Jsonobject Jo =(Jsonobject) Json.tojson (person);8 returnJo;9}
5. Convert the JSON string to map.
1 /**2 * Convert JSON string to map3 * @paramjsonstr JSON string4 * @return5 */6 Public StaticMap<string,object>Changejsontomap (String jsonstr) {7map<string,object> map =NewHashmap<string,object>();8Jsonobject Jo =Json.parseobject (JSONSTR);9 for(String str:jo.keySet ()) {Ten map.put (str, jo.get (str)); One } A returnmap; -}
6, write test class.
protected LongStartTime; protected LongEndTime; Public Static FinalString Json_to_bean = "{" + "' name ': ' Ogawa 94 '," + "' Age ':-" + "' Gender ': ' Boy '," + "' hasgirlfriend ': false," + "' interest ': ' Coding '" + "}"; Public StaticString str = "{\" person\ ": {" + "\" name\ ": \" Ogawa 94\ ", \" age\ ": 18,\" gender\ ": \" boy\ "," + "\" Hasgirlfriend \ ": false,\" interest\ ": \" coding\ "}}"; Public StaticString Strarray = "{\" person\ ": [" + "{\" name\ ": \" Ogawa 94_2 No. \ "},{\" name\ ": \" Ogawa 94_3 no. \ "}]}"; @Before Public voidSetUp ()throwsException { This. starttime=System.currenttimemillis (); System.out.println ("========= Start testing ==========="); } @After Public voidTearDown ()throwsException { This. EndTime =System.currenttimemillis (); System.out.println ("Test Time:" + (endtime-startTime)); System.out.println ("========= Test End ==========="); } @Test Public voidTestchangepersontobean () { person person=Jsonutils.changejsontobean (Json_to_bean); SYSTEM.OUT.PRINTLN (person); System.out.println ("Name:" +person.getname ()); System.out.println ("Age:" +person.getage ()); System.out.println ("Gender:" +Person.getgender ()); } @Test Public voidTestchangebeantojson () { person person=NewPerson (); Person.setname ("Ogawa 94"); Person.setage (18); Person.setgender ("Boy."); Person.sethasgirlfriend (false); Person.setinterest ("Coding"); Jsonobject Jo=Jsonutils.changebeantojson (person); System.out.println (Jo); } @Test Public voidTestchagejsontomap () {Map<String,Object> map =Jsonutils.changejsontomap (Json_to_bean); SYSTEM.OUT.PRINTLN (map); } @Test Public voidTestchangestringtojson () {jsonobject Jo=Jsonutils.changestringtojson (str); System.out.println (Jo); } @Test Public voidTestchangestringtoarray () {Jsonarray ja=Jsonutils.changestringtoarray (Strarray); System.out.println (JA); System.out.println (Ja.get (0)); }
7, running results: The above five test cases can be compiled and run normally, here is not explained.
The article starts with my personal public number: Yue Lok book . I like to share the songs I've heard, the movies I've seen, the books I've read, the code I've been reading, the deep-night meditation. Look forward to your attention! This blog post for my original, reproduced please indicate the source!
JSON summary and the actual JSON operation of the Fastjson package