Conversion between JSON string and object

Source: Internet
Author: User

 PackageASI;Importjava.util.ArrayList;ImportCom.alibaba.fastjson.JSON;Importcom.alibaba.fastjson.JSONException;ImportCom.alibaba.fastjson.JSONObject; Public classTestjsonobject {PrivateString name;PrivateString sex;  PublicString GetName () {returnname;}  Public voidsetName (String name) { This. Name =name;}  PublicString Getsex () {returnsex;}  Public voidsetsex (String sex) { This. Sex =sex;} //JSON string converted to Jsonobject class  Public Staticjsonobject Json2object (String json) {Jsonobject Object=(Jsonobject) jsonobject.parseobject (JSON); returnobject;} //JSON list converted to arraylist<jsonobject>  Public StaticArraylist<jsonobject>Jsonlist2object (String jsonlist) {ArrayList<JSONObject> objectlist= (arraylist<jsonobject>) jsonobject.parseobject (jsonlist,arraylist.class); returnObjectList;} //convert object to JSON  Public StaticString Object2json (Object o) {returnjson.tojsonstring (o); }   Public Static voidMain (string[] args) {String json= "{\" username\ ": \" bonnie\ ", \" Listdata\ ": [{\" address\ ": \" Shijingshan district \ ", \" street\ ": \" Yuhua road \ "},{\" address\ ": \" Haidian district \ ", \" Street\ ": \" Zhongshan road \ "}]}"; Try{   //1. Test JSON into ObjectJsonobject object=Json2object (JSON); String username= (String) object.get ("username");      SYSTEM.OUT.PRINTLN (username); String ListData=string.valueof (Object.get ("ListData")); //(String) object.get ("ListData");System.out.println (ListData); ArrayList<JSONObject> objectlist=Jsonlist2object (ListData);  for(intI=0;i<objectlist.size (); i++) {System.out.println (String) ((Jsonobject) (Objectlist.toarray ()) [i]). Get ("Address")); System.out.println (String) ((Jsonobject) (Objectlist.toarray ()) [i]). Get ("Street")); }   //2. Test object into JSONTestjsonobject t=NewTestjsonobject (); T.setname ("Bonnie"); T.setsex ("Female");  System.out.println (Object2json (t)); }Catch(jsonexception e) {e.printstacktrace (); }       }}

Note To import the jar:

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.2</version>
</dependency>

Conversion between JSON string and object

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.