json format
JSON format: http://www.json.org/
For the relationship between python and JSON, please refer to: http://docs.python.org/library/json.html
There are two types of JSON construction Structure:
1. A collection of name/value pairs. In different languages, it is understood as an object, a record, a struct, a dictionary, a hash table, a keyed list, or an associative array. array).
2. An ordered list of values. In most languages, it is understood as an array.
Basic Example
Simply put, JSON can convert a set of data represented in a JavaScript object into a string. This string can then be easily passed between functions, or strings from The web client passes it to the server-side program. This string looks a little weird, but JavaScript can easily interpret it, and JSON can represent more complex structures than name/value pairs. For example, arrays and complex objects can be represented rather than just simple lists of keys and values.
Represent name / value pair
In its simplest form, you can use the following JSON to represent "name / value pair":
{ "firstName": "Brett" }
This example is very basic, and actually Takes up more space than the equivalent plain text name/value pair:
firstName=Brett
However, JSON shows its value when multiple name/value pairs are strung together . First, you can create records containing multiple "name / value pairs", such as:
{ "firstName": "Brett", "lastName": "McLaughlin", "email": "aaaa" }
From the syntax perspective At first glance, this isn't a huge advantage over name/value pairs, but in this case JSON is easier to use and more readable. For example, it makes it clear that the above three values are part of the same record; the curly braces make the values somehow related.
Representing an array
When it is necessary to represent a set of values, JSON can not only improve readability, but also reduce complexity. For example, suppose you want to represent a list of people's names. In XML, many start and end tags are required; if you use typical name/value pairs (like the ones you saw in previous articles in this series), a proprietary data format must be created , or change the key name to the form person1-firstName.
If using JSON, just group multiple records together with curly braces:
{ "people": [
{ "firstName": "Brett", "lastName": "McLaughlin", "email ": "aaaa" },
{ "firstName": "Jason", "lastName": "Hunter", "email": "bbbb"},
{ "firstName": "Elliotte", "lastName": "Harold", "email": "cccc" }
]}
This is not difficult to understand. In this example, there is only one variable called people, and the value is an array containing three entries, each entry is a record for a person, containing a first name, last name, and email address. The above example demonstrates how to use parentheses to combine records into a single value. Of course, you can use the same syntax to represent multiple values (each value contains multiple records):
{ "programmers": [
{ "firstName": "Brett", "lastName": "McLaughlin", "email ": "aaaa" },
{ "firstName": "Jason", "lastName": "Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName": "Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", " lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
Here Most notably, the ability to represent multiple values, each of which in turn contains multiple values. However, it should also be noted that the actual name/value pairs in the record can differ between different main entries (programmers, authors, and musicians). JSON is fully dynamic, allowing the way data is represented to change in the middle of the JSON structure.
There are no predefined constraints that need to be followed when processing data in JSON format. Therefore, within the same data structure, the way the data is represented can be changed, and the same thing can even be represented in different ways.
Format Application
After mastering the JSON format, using it in JavaScript is very simple. JSON is a native JavaScript format, which means that processing JSON data in JavaScript does not require any special API or toolkit.
Assign JSON data to a variable
For example, you can create a new JavaScript variable and then directly assign the JSON format data string to it:
var people = { "programmers": [ { "firstName": " Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName" : "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton" , "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
This is very simple; now people contains The data in JSON format seen earlier. However, this isn't enough as the way to access the data doesn't seem obvious yet.
Accessing data
Although it may not seem obvious, the long string above is actually just an array; you can easily access this array by putting it into a JavaScript variable. In fact, just use dot notation to represent array elements. So, to access the last name of the first entry in the programmers list, just use the following code in JavaScript:
people.programmers[0].lastName;
Note that array indexing is zero-based. So, this line of code first accesses the data in the people variable; then moves to the entry called programmers, then moves to the first record ([0]); and finally, accesses the value of the lastName key. The result is the string value "McLaughlin".
Here are a few examples using the same variable.
people.authors[1].genre // Value is "fantasy"
people.musicians[3].lastName // Undefined. This refers to the fourth entry, and there isn't one
people.programmers[ 2].firstName // Value is "Elliotte"
Using this syntax, you can process any JSON format data without using any additional JavaScript toolkit or API.
Modify JSON data
Just as you can access the data with periods and brackets, you can also easily modify the data in the same way:
people.musicians[1].lastName = "Rachmaninov";
After converting the string to After the JavaScript object is created, you can modify the data in the variable like this.
Convert back to string
Of course, all data modifications are of little value if you can’t easily convert the object back to the text format mentioned in this article. This conversion is also very simple in JavaScript:
String newJSONtext = people.toJSONString();
That’s it! You now have a text string that can be used anywhere, for example, as a request string in an Ajax application.
More importantly, any JavaScript object can be converted into JSON text. It is not only possible to handle variables originally assigned with JSON strings. To convert an object named myObject, simply execute a command of the same form:
String myObjectInJSON = myObject.toJSONString();
This is the biggest difference between JSON and the other data formats discussed in this series. If you use JSON, you only need to call a simple function to get the formatted data, which can be used directly. For other data formats, conversion between raw and formatted data is required. Even if you use an API like the Document Object Model (which provides functions to convert your own data structures into text), you need to learn the API and use the API's objects instead of using native JavaScript objects and syntax.
The final conclusion is that if you are dealing with a large number of JavaScript objects, then JSON is almost certainly a good choice, so that you can easily convert the data into a format that can be sent to the server-side program in the request.
Specific form
1. An object is an unordered collection of "name/value pairs". An object starts with "{" (left bracket) and ends with "}" (right bracket). Each "name" is followed by a ":" (colon); "name/value" pairs are separated by "," (comma). (As shown in the figure, the way the data is represented in the figure is similar to a non-deterministic automaton. People who have not learned the principles of compilation may find it difficult to understand. In fact, it is also in the form of a regular expression. The same below)
2. An array is an ordered collection of values. An array starts with "[" (left bracket) and ends with "]" (right bracket). Values are separated by "," (comma).
3. Value can be a string enclosed in double quotes, a number, true, false, null, an object
4. A string is composed of A collection of any number of Unicode characters enclosed in double quotes, escaped with backslashes. A character is a single character string. Strings are very similar to C or Java strings.
5. Numbers are also very similar to C or Java numbers. Remove unused octal and hexadecimal formats. Removed some encoding details.
For more articles related to json format, please pay attention to the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
