


What is the JSON format? Introduction to the usage of JSON format (with code)
The content of this article is about what is the JSON format? The usage introduction of JSON format (with code) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1: JSON format definition
JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy for humans to read and write. It is also easy for machines to parse and generate. It was proposed by Douglas Crockford in 2001 to replace the cumbersome and cumbersome XML format.
2. JSON format rules
The value of composite type can only be an array or object, not a function, regular expression object, or date object.
There are only four simple types of values: string, numerical value (must be expressed in decimal), Boolean value and null (NaN, Infinity, -Infinity and undefined cannot be used).
Strings must be expressed in double quotes, single quotes cannot be used.
The key name of the object must be placed in double quotes.
No comma can be added after the last member of an array or object.
Empty arrays and empty objects are both qualified JSON values, and null itself is also a qualified JSON value
以下是合格的 JSON 值。 ["one", "two", "three"] { "one": 1, "two": 2, "three": 3 } {"names": ["张三", "李四"] } [ { "name": "张三"}, {"name": "李四"} ]
以下是不合格的 JSON 值。 { name: "张三", 'age': 32 } // 属性名必须使用双引号 [32, 64, 128, 0xFFF] // 不能使用十六进制值 { "name": "张三", "age": undefined } // 不能使用undefined { "name": "张三", "birthday": new Date('Fri, 26 Aug 2011 07:13:10 GMT'), "getName": function() { return this.name; } } // 不能使用函数和日期对象
3. Processing JSON format Data method
1, JSON.Stringify
1) Purpose
is used to convert a value into a string. The string conforms to JSON format and can be restored by the JSON.parse method.
2) Give an example
JSON.stringify('abc') // ""abc"" JSON.stringify(1) // "1" JSON.stringify(false) // "false" JSON.stringify([]) // "[]" JSON.stringify({}) // "{}" JSON.stringify([1, "false", false]) // '[1,"false",false]' JSON.stringify({ name: "张三" }) // "{"name":"张三"}"
3) Summary
First write "" and convert it to string format, and then Convert the content that needs to be converted according to the rules of json format. Add "" to "", and then put the converted content inside "" and call it a day.
4) Special case
Contents not supported by json format will be filtered, divided into 3 situations
Original object
Original object members If the value is undefined, a function or an XML object, this member will be filtered
var obj = { a: undefined, b: function () {} }; JSON.stringify(obj) // "{}"
2. Array
If the member of the array is undefined, a function or an XML object, these values will be converted to null
var arr = [undefined, function () {}]; JSON.stringify(arr) // "[null,null]"
3. Regular object
Regular objects will be converted into empty objects.
JSON.stringify(/foo/) // "{}"
2. JSON.parse()
1) Purpose
The JSON.parse method is used to convert JSON format string Convert into object.
2) For example
JSON.parse('{}') // {} JSON.parse('true') // true JSON.parse('"foo"') // "foo" JSON.parse('[1, 5, "false"]') // [1, 5, "false"] JSON.parse('null') // null var o = JSON.parse('{"name": "张三"}'); o.name // 张三
3) Special case
If the incoming string is not in a valid JSON format, JSON. The parse method will report an error.
JSON.parse("'String'") // illegal single quotes // SyntaxError: Unexpected token ILLEGAL
4) Pitfalls encountered
One of the red boxes contains single quotes and the other contains double quotes. Both of them run correctly.
This represents the quotation marks used to indicate that the value is a string. You can use single or double quotation marks, but the quotation marks indicating that the content is in json format must be written in double quotation marks
The above is the detailed content of What is the JSON format? Introduction to the usage of JSON format (with code). For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
