Home >Web Front-end >JS Tutorial >JSON Syntax and Tips

JSON Syntax and Tips

Joseph Gordon-Levitt
Joseph Gordon-LevittOriginal
2025-02-23 09:36:45936browse

JSON basic knowledge quick review and practical skills

JSON Syntax and Tips

JSON Introduction: JavaScript Object Notation (JSON) is a text format used to serialize structured data. It comes from JavaScript object literals.

JSON can represent four basic types (strings, numbers, booleans, and nulls) and two structured types (objects and arrays).

JSON object example:

This is a JSON object containing attributes:

<code class="language-json">{
  "myObject": {
    "name": "obi wan kenobi",
    "weapons": "lightsaber",
    "specialPowers": "the force"
  }
}</code>

This is a JSON object containing objects:

<code class="language-javascript">{
  "Africa": {}
}</code>

JSON array example:

This is a JSON object containing a JSON array:

<code class="language-json">{
  "myObject": {
    "name": "obi wan kenobi",
    "weapons": ["lightsaber", "smoke grenade", "knife", "jedi things"],
    "specialPowers": "the force"
  }
}</code>

This is a JSON array containing two objects:

<code class="language-json">{
  "africaLagos": [
    {
      "from": -377711769600000,
      "to": -1588464816000,
      "dst": false,
      "offset": 816,
      "name": "LMT"
    },
    {
      "from": -1588464816000,
      "to": 253402300799000,
      "dst": false,
      "offset": 3600,
      "name": "WAT"
    }
  ]
}</code>

More JSON examples and usage tips:

JSON Analysis:

Blindly evaluate any JSON strings due to safety risks. It is best to use the eval() method, which has been part of the language since ES5 and is provided natively by the JavaScript engine in modern browsers. In jQuery, there is the JSON.parse() method: parseJSON()

The reverse operation of the
<code class="language-javascript">// 一个输入 JSON 字符串
var jstr = '{"mykey": "my value"}';
var data = jQuery.parseJSON(jstr);
console.log(data.mykey); // "my value"</code>

method is JSON.parse(). It takes any object or array (or primitive type) and serializes it into a JSON string. JSON.stringify()

<code class="language-javascript">var dog = {
  name: "Fido",
  dob: new Date(),
  legs: [1, 2, 3, 4]
};
var jsonstr = JSON.stringify(dog);
// jsonstr 现在是:
// {"name":"Fido","dob":"2010-04-11T22:36:22.436Z","legs":[1,2,3,4]}</code>

Characters that must be escaped in JSON string:

    Double quotes "
  • Forward slash /
  • Backslash
  • Line newline character n
  • Carriage return r
  • Tab t

JSON Syntax and Skills FAQ:

(The FAQ part is omitted here because the article is too long and has a high degree of repetition with the original text. Part of the FAQ can be retained or supplemented as needed, and synonyms and statement adjustments are performed to achieve pseudo-original purpose. )

The above is the detailed content of JSON Syntax and Tips. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn