java-generating JSON data in any format

Source: Internet
Author: User

A recent study of Java stuff. Previously, by their own groping, the implementation of the Java object into the JSON format of the data, the ability to return to the front end. The Jsonobject.fromobject (object) method was used to replace the Java object with the JSON format. That is, there is a Java entity class first, such as user. Then the list data is isolated from the database, that is, a list, where each piece of data is a user entity object. If the front-end requirements change and you need to return more than one field in the current interface, you need to modify the user entity class to add new fields. In this way, all the interfaces that use this user entity class will have the new field added to the JSON data returned by the interface. Later found that you can use a method to dynamically splice the required fields as needed.

1. Demo
 PackageCom.lin.domain;ImportNet.sf.json.JSONArray;ImportNet.sf.json.JSONObject; Public classTest { Public Static voidMain (string[] args)throwsException {jsonobject createjsonobject=Createjsonobject ();    System.out.println (Createjsonobject); }         //Create a Jsonobject object    Private Staticjsonobject Createjsonobject () {jsonobject result=NewJsonobject (); Result.put ("Success",true); Result.put ("TotalCount", "30"); Jsonobject User1=NewJsonobject (); User1.put ("id", "12"); User1.put ("Name", "Zhang San"); User1.put ("Createtime", "2017-11-16 12:12:12"); Jsonobject User2=NewJsonobject (); User2.put ("id", "13"); User2.put ("Name", "John Doe"); User2.put ("Createtime", "2017-11-16 12:12:15"); Jsonobject Department=NewJsonobject (); Department.put ("id", 1); Department.put ("Name", "technical department"); User1.put ("Department", department); User2.put ("Department", department); //returns a Jsonarray objectJsonarray Jsonarray =NewJsonarray (); Jsonarray.add (0, User1); Jsonarray.add (1, User2); Result.element ("Data", Jsonarray); returnresult; } }

The JSON data returned:

2. Interface Demo

The following is a real Java interface that queries data from a database

@ResponseBody @RequestMapping (value= "/getrolemenulist.do", method=requestmethod.get) Public voidGetrolemenulist (httpservletrequest req, httpservletresponse res, Integer Roleid)throwsioexception{Res.setheader ("Content-type", "Application/json;charset=utf-8"); Res.setcharacterencoding ("UTF-8"); Reslistdata Rld=NewReslistdata (); Jsonobject result=NewJsonobject (); Try{Map<string, object> params1 =NewHashmap<>(); Params1.put ("Roleid", Roleid); Params1.put ("MenuLevel", 1); List<RoleJuri> fmenulist = Rjservice.getrolemenulist2 (PARAMS1);//level MenuJsonarray firstlist =NewJsonarray ();  for(inti=0; I<fmenulist.size (); i++) {Rolejuri Firstmenu=Fmenulist.get (i); Jsonobject Firstresult=NewJsonobject (); Firstresult.put ("id", Firstmenu.getid ()); Firstresult.put ("Name", Firstmenu.getmenuname ()); Firstresult.put ("url", Firstmenu.getmenuurl ()); Map<string, object> params2 =NewHashmap<>(); Params2.put ("Roleid", Roleid); Params2.put ("Menupid", Firstmenu.getmenuid ()); List<RoleJuri> smenulist = Rjservice.getrolemenulist2 (PARAMS2);//Level Two menuJsonarray secondlist=NewJsonarray ();  for(intj=0; J<smenulist.size (); J + +) {Rolejuri Secondmenu=Smenulist.get (j); Jsonobject Secondresult=NewJsonobject (); Secondresult.put ("id", Secondmenu.getid ()); Secondresult.put ("Name", Secondmenu.getmenuname ()); Secondresult.put ("url", Secondmenu.getmenuurl ());                Secondlist.add (Secondresult); } firstresult.put ("Children", secondlist);            Firstlist.add (Firstresult); }            if(Fmenulist.size () > 0) {//query to the first level menuResult.put ("Success", 1); Result.put ("Data", firstlist); }Else{//The first level menu is not queriedResult.put ("Success", 0); Result.put ("Data",NewArray ()); Result.put ("Error", "No access to menu data"); }        } Catch(Exception e) {result.put ("Success", 0); Result.put ("Data",NewArray ()); Result.put ("Error", "Server Run Error");    } res.getwriter (). Write (result.tostring ()); }

The JSON data returned

java-generating JSON data in any format

Related Article

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.