Project Reflection--Reservation System of College Student Activity Center - [Interconversion of Json, Json String, Json Array, Object and List]

Conversion of Json, Json string, Json array, Java object, List

1. Login operation

[Front end (form form form - > Json - > Json string) - - > Backend
Front-end -> [Back-end (Json string - > Java object)]

(1) Front end (form form form - > Json - > Json string)
The front end uses ajax to POST a Json string to the back end interface. First, the data from the form is captured and encapsulated into Json:

    var loginname=document.getElementById("loginname");
    var loginpassword=document.getElementById("loginpassword");
    //Package json
    var jsonreq={
        loginname:loginname.value,
        password:loginpassword.value
    };

Then Json is converted into a Json string and sent:

    var jsondate = JSON.stringify(jsonreq);
        ...
    xmlHttp.send(jsondate);

(2) Backend (Json string - > Java object)
Front-end to back-end POST data is a Json string, when the background receives this string, it needs to be converted into Java objects for operation. Receive:

     @RequestMapping(value = "/login")
     @ResponseBody
     public String login(@RequestBody String reqjson) {}

Transformation:

        //json string rotation json
        User user = ResultContent.jsonstring2obj(reqjson);
    //json String Rotation User Object
    public static User jsonstring2obj(String jsonrs){
        JSONObject jsonObject=JSONObject.fromObject(jsonrs);
        User user=(User)JSONObject.toBean(jsonObject, User.class);
        return user;
    }

2. Query all active operations

[Backend (List <> - > Json Array - > Josn String)] --> Front End
Backend -> [Front end (Json string - > Json array)]

(1) Backend (List <> - > Json array - > Josn string)
How does the back end translate the List <> queried from the database into a Json string?

List<Activi> list = activiService.selectallactivi();
return ResultContent.ResultList2JsonString(list);

Start the transition:

    //List < Object > to Json string
    public static String ResultList2JsonString(List lists){
        JSONArray listArray= JSONArray.fromObject(lists);
        return listArray.toString();
    }

(2) Front end (Json string - > Json array)
When the front end removes multiple GET data, the back end converts all data into Json strings. The front end needs to convert the Json strings returned by the back end into Json arrays:

var result = xmlHttp.responseText; //Response Success s
//JSON String - --"JSON Array
activiorderjsonlist = eval(result);

This Json array can then be manipulated accordingly:

    for(var i=0;i<activiorderjsonlist.length;i++) {
        if(activiorderjsonlist[i].orderplace==place){
            var html = "";
            if (activiorderjsonlist[i].approvetype == 1) {
                approvetype = "Approval and approval";
            } else if (activiorderjsonlist[i].approvetype == 2) {
                approvetype = "Failure to pass the examination and approval";
            } else {
                approvetype = "Waiting for approval";
            }
            }}}

3. Backend return request control

[Backend (java object, common data - > Json - > Josn string) - - > Front end
Backend -> [Front end (Json string - > Json object)]

(1) Backend (common data - > Json - > Josn string)
The back end creates Json objects for put ting, object - > Json - > Json strings:

//Result control
    public static String result2jsonstring(String rs,String errormsg,Object object){
        JSONObject json=null;
        if(object!=null){//When an entity class is returned
            //Object to Json
            json = JSONObject.fromObject(object);
            json.put("REQUESTRS",rs);
            if(errormsg!=null){
                json.put("ERRORMSG",errormsg);
            }
        }else {//When the return value does not contain the entity class
            Map map=new HashMap();
            map.put("REQUESTRS",rs);
            if(errormsg!=null){
                map.put("ERRORMSG",errormsg);
            }
            json = JSONObject.fromObject(map);
        }
        //Josn to Json string
        return json.toString();
    }

(2) Front end (Json string - > Json object)
When the back end returns the result, it encapsulates the data as Json and replaces it with Json string to return to the front end. After the front end receives the result, it needs to be converted into Json object:

var result=xmlHttp.responseText;//Response success
//JSON String - - "JSON Object"
var jsonresult=JSON.parse(result);

The Json object can then be input manipulated:

if("SUCCESS" == jsonresult.REQUESTRS){
                alert("Welcome!"+jsonresult.username);
                //Jump to the appropriate interface
                if(2==jsonresult.usertype){//Daiwu Administrator
                    window.open("adminis.html","_blank");
                }else {
 window.location.href="allactivi.html";
                }
 }else {
    alert("Logon failed!"); 
 }

Tags: JSON Java Front-end Attribute

Posted on Thu, 27 Dec 2018 11:27:06 -0800 by shadow1200