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": "Tom", "sex": "male", "age": "24"} / / JSON object
console.log ( person.name );//output tom in the console
alert(typeof(person));//object
The above is the JSON object. is a call to attribute 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. So what is the concept of a JSON string?
var b=‘{"name":"2323","sex":"afasdf","age":"6262"}‘;//json String
console.log(b);//{"name":"2323","sex":"afasdf","age":"6262"}
alert(typeof(b));//string
The above is B is a string, is also a JSON string, the reason is called a JSON string, because the format of the string is in JSON format, so called the JSON string, the third line of code also matches the type string.
Conversion of JSON strings and JSON objects
JSON string to JSON object, call the Parse method:
var b=‘{"name":"2323","sex":"afasdf","age":"6262"}‘//json string
var bToObject=JSON.parse(b); console.log(bToObject.name);//2323
JSON object to JSON string:
var a={"name":"tom","sex":"male","age":"24"}//json object
var aToString=JSON.stringify(a);
console.log(aToString);//{"name":"tom","sex":"male","age":"24"}
SPRINGMVC accepts JSON string types.
During a period of time to do projects, and friends, based on the development of rest-style, has been understood as the front-end Ajax submitted by a JSON object, backstage to @requestbody accepted JSON object, and then I found myself wrong, not actually, The front-end is a JSON-formatted string in the background, and here's an example:
<script type="text/javascript">
$(document).ready(function(){
var saveDataAry=[];
var data1={"userName":"test","address":"gz"};
var data2={"userName":"ququ","address":"gr"};
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":"test","address":"gz"},
{"userName":"ququ","address":"gr"}
]
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.
Background acceptance
@Controller
@RequestMapping(value = "saveUser", method=RequestMethod.POST )
@ResponseBody
public void saveUser(@RequestBody List<User> users) {
userService.batchSave(users);
}
The background is encapsulated in the @requestbody with aList<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