JSON conversion process

Ajax foreground object passed to the background:

data preparation

Converting js object or object json json string passed in Ajax, in the background and then converted into a string json json object or objects and then converted into a java front and rear ends configured in the same data structure, with direct application / json send body content.

Copy the code
 1 was student = {
 2     name:"abc",
 3     age:12,
 4     no:"123"
 5 };
 6 
 7     console.log(student);
 8 // convert js object passing via ajax json string, then the string into json json objects and then converted to java objects in the background
 9 
10     student = JSON.stringify(student);
Copy the code

Ajax: Send the front

Copy the code
 1 $.ajax({
 2 url: url,
 3         type : "POST",
 4         data : {
 5 sendData: "Pass the following json string"
 6                 jsonStr: student
 7                 },
 8         async : isAsync,
 9         dataType:data_type,
10         beforeSend : beforeSendFun,
11         success : function(return_data) {
12             successFun(return_data);
13         },
14         error : function(XMLHttpRequest, textStatus, errorThrown) {
15 alert ( "Request processing failed");
16         }
17     });
Copy the code

Background Analysis:

Copy the code
String sendData = request.getParameter("sendData");

if (sendData.equals ( "pass below json string")) {

String jsonStr = request.getParameter("jsonStr");

JSONObject student_json = new JSONObject () fromObject (jsonStr);. // json string into the target json
Student student = (Student) JSONObject.toBean (student_json, Student.class); // then the object into a Student object json }
Copy the code

(1) the originating object var abc = new {name = "novice tutorial", site = "http://www.runoob.com"}; JSON (abc)

console.log(ds);

the console.log (typeof (DS)); Object
the console.log (ds.name); novice tutorial

Objectname: "菜鸟教程"site: "http://www.runoob.com"__proto__: Object
ThemeList.js:24 {"name":"菜鸟教程","site":"http://www.runoob.com"}
ThemeList.js:27 "{\"name\":\"菜鸟教程\",\"site\":\"http://www.runoob.com\"}"
ThemeList.js:28 "\"{\\\"name\\\":\\\"菜鸟教程\\\",\\\"site\\\":\\\"http://www.runoob.com\\\"}\""

 

(2) originating string string abc = "{\" name \ ": \" novice Tutorial \ ", \" site \ ": \" http: //www.runoob.com \ "}"; JSON (abc )

var mm=JSON.parse("\"{\\\"name\\\":\\\"菜鸟教程\\\",\\\"site\\\":\\\"http://www.runoob.com\\\"}\"")
console.log(typeof (mm));string
var ll = JSON.parse(mm);object
console.log(ll.name);菜鸟教程

Guess you like

Origin www.cnblogs.com/bwdblogs/p/11097378.html
Recommended