Java to JSON or JSON to JavaBean

Source: Internet
Author: User

Practice today, put it here, and add it later.

The jar package used here is Net.sf.json.JSONObject

Package Yh.test.t1118;import net.sf.json.jsonarray;import net.sf.json.jsonobject;import yh.com.entity.User; Import Java.util.hashmap;import java.util.list;import java.util.Map;/** * Created by Administrator on 2015/11/18.*/ Public classTest { Public Static voidMain (string[] args) {//Javatojson ();//Jsontojava ();test1 (); }    /** * java to JSON*/     Public Static voidJavatojson () {String json="{\ "json\": \ "json\"}"; Jsonobject Jsonobj=Jsonobject.fromobject (JSON); Map<string, string> user =NewHashmap<string, string>(); User.put ("name","Jack"); User.put (" Age"," +"); User.put ("Sex","male"); User.put ("Job","Students"); Jsonobj.put ("Users", user); System. out. println ("------Start outputting JSON------"); System. out. println (Jsonobj); }    /** * JSON to Java*/     Public Static voidJsontojava () {String json="{x: ' 1 ', y: ' 2 ', userId: ' Element:[{id ', ' 123 ', Name: ' Haha '},{id: ' 456 ', Name: ' hehe '}]}";//jsonobject jsonobject = new Jsonobject ();Jsonobject Jsonobject =Jsonobject.fromobject (JSON); Jsonobject.element ("name","Limei"); Jsonobject.element ("Sex","female"); Jsonobject.accumulate ("JJ","SS"); Map<String,String> map =NewHashmap<string, string>(); Map.put ("a","A"); Map.put ("b","B"); Map.put ("C","C"); Jsonobject.element ("User", map);//System.out.println (jsonobject.tostring ());Object o= Jsonobject.Get("User");//System.out.println ("==================>" +o);//System.out.println ("Name:" +jsonobject.get ("name"));Jsonarray Jsonarray = Jsonobject.getjsonarray ("element");  for(inti =0; I<jsonarray.size (); i++) {System. out. println (Jsonarray.Get(i)); }//Object object = Jsonobject.tobean (jsonobject);//System.out.println (object.tostring ());    }    /******************* deserializing json into javabean*****************/     Public Static voidtest1 () {String jsonstr="{x:1,\ "userid\": \ "112\", Element:[{id: ' 123 ', Name: ' Haha '},{id: ' 456 ', Name: ' hehe '}]}"; Map<String,Class<?>> m =NewHashmap<string,class<?>>(); M.put ("x", Integer.class); M.put ("userId", String.class); M.put ("element", Element.class); Jsontobean Mybean= (Jsontobean) Jsonobject.tobean (Jsonobject.fromobject (JSONSTR), Jsontobean.class, M); System. out. println ("x:"+mybean.getx ()); System. out. println ("userId:"+Mybean.getuserid ());  for(Element e:mybean.getelement ()) {System. out. println (E.getid () +","+e.getname ()); }    }}

Java to JSON or JSON to JavaBean

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.