On the previous talk about what is JSON, then this is to bring you the use of JSON in Web projects, that is, the encapsulation and parsing of JSON.
This diagram is part of the database
One, JSON encapsulation
The so-called JSON encapsulation refers to the conversion of data from a database into a JSON-formatted string in a servlet.
So what is a JSON-formatted string?
- A string in JSON format means that the contents of a string are fully formatted with the JSON data
This is the result of the output of the JSON format string in the servlet that has been encapsulated in the console output.
How to Encapsulate
- In my own web, I originally wanted to use the original JSON to encapsulate, but the project manager introduced me to the Gson, so I used Gson to encapsulate
Code (servlet)
/*** * Retrieve grievance information via ID and upload to page Ajax call * Admin *@return */ Publicstring Getlistappeal (httpservletrequest request, HttpServletResponse response) {string id= Request.getparameter ("appeal_id"); Try{Response.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out=Response.getwriter (); Gson Gson=NewGson (); System.out.println ("I am a JSON format string:" +Gson.tojson (appealservice.getlistappeal (ID))); Out.print (Gson.tojson (appealservice.getlistappeal (ID))); Out.close (); } Catch(IOException e) {e.printstacktrace (); } return NULL;//NULL represents an Ajax call }
Appealservice is the service layer implementation class, mainly to call DAO for JBDC
Getlistappeal (ID) as
After the argument is an object, the argument can be List,map,bean (custom Class), and so on. This method returns a JSON-encapsulated string
Second, the analysis
JSON parsing refers to the process by which a JSP page receives a JSON string sent by the servlet and translates it into a JSON object.
- In parsing we need to use the method of eval ()
Code:
$.ajax ({URL:"Appeal.do", data:{"Method": "Getlistappeal", "appeal_id": ID}, DataType:"JSON", type:"POST", Success:function(Result) {//result--> receive parameters returned by the servlet layerConsole.log ("not processed = = = =" +result); varJSON = eval (result);/*JSON parsing*/Console.log ("Processed = = =" +JSON); //get the values in the JSON data$ ("#id"). Val (json[0].appeal_id); $("#userAccount"). Val (json[0].user_account); $("#type"). Val (json[0].appeal_type); $("#content"). Val (json[0].appeal_content); $("#time"). Val (json[0].appeal_time); } });
Results:
It is worth reminding that in the call Gson.tojson () mode, if the parameter is map may have some problems, that is, during the parsing process, follow the above steps can not be converted to JSON object OH.
Code:
$.ajax ({URL:"Orderservlet.do?method=getmapordernumbygame", datatype:"JSON", type:"POST", Async:false, Success:function(Result) {/*Alert (eval (result))*/Console.log ("Not processed = =" "+result); Console.log ("process once = = =" +eval (result)); varJSON =eval (eval (result)); Console.log ("Handling two times = = =" +JSON); option.series[0].data =JSONvarS= ""; for(varJinchJSON) {s+=","+Json[j].name} s=S.SUBSTR (1); Option.legend.data=s.split (","); mychart.setoption (option); } });
Results:
As can be seen, when the JSON string is not processed, the string has an escape character, after processing once, and the above step is the same, so there are two times to parse.
The above for today's content, if you need to know more in-depth knowledge, please enter the Cicada Hall community: http://www.zhiliaotang.com/portal.php;
[Cicada Hall Learning note]_json Data Operation 2nd (JSON encapsulation and parsing)