Comparison of JSON strings and XML

Source: Internet
Author: User

JSON is an AJAX foreplay that uses JSON to transmit data on both the front and back ends.

JSON is a lightweight data interchange format,

(JavaScript Objects Notation, JS object tag), related to JavaScript,

Text formats that are independent of the programming language to store and represent data,

Json.dumps () Serializes the string and puts it into the disk,

Data types supported by Python, Int,str,list,tuple,dict,set

Single quotes in Python, no difference from double quotes

In JSON, the data format is double quotes,

 info = { " name   ": "  gu  , "  age   ' : 10}   Print  (type (info)) #  <class ' dict ' >  import   Jsonret  =json.dumps (info)  print  (Type (ret)) #  <class ' str ';  print  (ret) #  {"name": "Gu", "Age": ten}  

Writes serialized data to a file, Json.dumps () gets a string,

# writes the serialized data to the file, the filename is info.txtf=open ('info.txt','w' ) Data=f.write (ret)# Store the serialized RET file in a file f.close ()

There's info.txt on the disk.

Read JSON-formatted data from a file on disk,

# read JSON-formatted files from disk f=open ('info.txt')# read files,data=f.read ()  # get data in JSON format data dict = json.loads (data)# deserialization data from JSON format to get Python format, if The data that is to be deserialized must be in JSON format, double quotes only,print(dict['name'])#  Gu

----

JSON objects and JSON strings.

A JSON object is a subset of JavaScript objects,

JS data type has, Null,number, Str,undefind,array (array), Data,bool, {"": "}

There is a null (empty) in the JSON, number,string,bool,array,{"": "}

A string in JSON that only double quotes are recognized,

Summary: The JSON string is the JS object plus double quotes,

Why single-column JSON data type, in the Web application, the front-end data exchange, the back end is the Python language, the front-end is the JS data, related to data exchange, or, Python language and the Java language communication, all in the JSON data format to exchange, all with the JSON string,

------

Stringify and the Parse method

Json.parse ("JSON string"), deserializes a JSON string into a JavaScript object, used in the front end,

In the front-end HTML file, Console.log (Json.parse (' {' name ': "Egon"} ')); Get a Object,name:egon

Json.stringify (JS object) that serializes the JavaScript value into a JSON string, used at the backend

Console.log (Json.stringify ([11,22,33])) Gets a string type,

Console.log (json.stringify ({' Name ': "Egon"})); Convert non-qualifying first to eligible JSON format

Console.log (json.stringify (typeof{"name": "Egon"}));

----

XML data format

Before 2001, data Exchange was XML-

Both XML and JSON use structured methods to tag data.

In XML there are nodes, node-nested nodes, which can also be collected, similar to the structure of HTML storage data

JSON is similar to saving data in a dictionary format,

JSON does not have a clear XML structure,

In the data exchange, JSON code is much smaller, XML in the large amount of data when the code is very large,

Online Format Code tool, Bejson,sojson, can put the messy JSON data format neatly formatted code, the hierarchy is clear,

Www.bejson.com

JSON data format, low code volume, easy to read,

In Ajax it is necessary to use JSON,

Ajax asynchronous JavaScript and XML, (asynchronous Javas and XML)

Comparison of JSON strings and XML

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.