The difference between a JSON object and a JSON string

Source: Internet
Author: User
Tags postgresql string to json


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


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.