JSON object
Sometimes the two concepts are confused when doing the project, especially when using SPRINGMVC, the background @requestbody accepts a JSON-formatted string, which must be a string.
Let's start with the JSON object, which begins with the concept of an object, which can be called by using the object property. For example:
var person={"name": "Zhangsan", "Sex": "Male", "Age": "}//json" Object alert (Person.name);//zhangsanalert (typeof person);// Object
The person is the JSON object. You can make a call to a property in this way perosn.name. The third line of code is to see the type of person, which is the object type.
JSON string
strings, strings in JavaScript that we often say are enclosed in single or double quotes.
var person= ' {' name ': ' Zhangsan ', ' sex ': ' Male ', ' age ': ' $ '} ';//json string alert (person);//{"name": "Zhangsan", "Sex": "Male", " Age ":"}alert "(typeof person);//string
The person is a JSON string, which is called a JSON string because the format of the string is in JSON format, and the third line of code matches the type string.
Conversion of JSON strings and Josn objects
JSON string to JSON object, call the Parse method:
var person= ' {' name ': ' Zhangsan ', ' sex ': ' Male ', ' age ': ' $ '} ';//json string var personobject = Json.parse (person); alert ( Personobject.name);//zhangsan
The JSON object is converted to a JSON string, calling the Stringify method:
var person={"name": "Zhangsan", "Sex": "Male", "Age": "$"};//json object var personstring = json.stringify (person); alert ( personstring);
SPRINGMVC accepts JSON string types.
When rest-based development is in SPRINGMVC, the front end should be a JSON-formatted string instead of a JSON object
<script type= "Text/javascript" > $ (document). Ready (function () { var savedataary=[]; var data1={"UserName": "Zhangsan", "Address": "BJ"}; var data2={"UserName": "Lisi", "Address": "NJ"}; Savedataary.push (data1); Savedataary.push (data2); $.ajax ({ type: "POST", URL: "User/saveuser", dataType: "JSON", contentType: "Application/json", data:JSON.stringify (saveData), success:function (data) { } }) ; </script>
The above code, first the push method encapsulates it into an array, which behaves in a format:
[ {"UserName": "Zhangsan", "Address": "BJ"}, {"UserName": "Lisi", "Address": "NJ"}]
Json.stringify (SaveData) converts it to a JSON string: At the same time the AJAX request is also specified datatype: "JSON", ContentType: "Application/json" This makes it easy to upload an object or list to the Java side.
Java background
@Controller @requestmapping (value = "Saveuser", Method=requestmethod.post) @ResponseBody public void Saveuser (@ Requestbody list<user> users) {
The background is encapsulated in the @requestbody with a List<User>
. Then enter the service layer.
GET, post method, according to the request header Content-type value to judge:
application/x-www-form-urlencoded, Optional (that is, not necessary, because the data of this situation @requestparam, @ModelAttribute can also be processed, of course @requestbody can also handle);
Multipart/form-data, cannot be processed (i.e. using @requestbody cannot process data in this format);
Other formats must be (other formats include Application/json, Application/xml, etc.). Data in these formats must be handled using @requestbody).
The difference between a JSON object and a JSON string