Quick Start: JSON Data Format

Quick Start: JSON Data Format

preface

JSON (JavaScript Object Notation) is a lightweight data exchange format commonly used for front-end and back-end data transmission and storage. It's easy to read and write, but also easy to parse and generate. This article will introduce the basic knowledge of JSON and help you quickly get started with the JSON data format.

What is JSON

  • JSON is a text format used to describe structured data.
  • It is based on JavaScript's syntax but can be parsed and generated by many other programming languages.
  • JSON uses key-value pairs to organize data, and the data is separated by commas ,.

JSON data structure

JSON uses the following data structures to represent data:

  1. Object (Object): wrapped in curly braces {} and consists of a set of key-value pairs. Keys and values ​​are separated by colons :, and key-value pairs are separated by commas ,.

    Example:

    {
          
          
      "name": "John Doe",
      "age": 30,
      "email": "[email protected]"
    }
    
  2. Array (Array): Wrapped in square brackets [], it consists of a set of values. Values ​​are separated by commas ,.

    Example:

    ["apple", "banana", "orange"]
    
  3. String: A group of characters enclosed by double quotes " or single quotes '.

    Example:

    "Hello, World!"
    
  4. Number: An integer or floating point number representing a numerical value.

    Example:

    42
    3.14
    
  5. Boolean: A value that represents true or false.

    Example:

    true
    false
    
  6. Null value (null): Indicates a null value.

    Example:

    null
    

Example: JSON object

The following is an example of a JSON object that represents personal information:

{
    
    
  "name": "John Doe",
  "age": 30,
  "email": "[email protected]",
  "hobbies": ["reading", "traveling"],
  "address": {
    
    
    "street": "123 Main St",
    "city": "New York",
    "country": "USA"
  }
}

In this example, the JSON object contains various fields for personal information:

  • "name": The value with key "name" is the string "John Doe".
  • "age": The value with key "age" is the number 30.
  • "email": The value with key "email" is the string "[email protected]".
  • "hobbies": The value with key "hobbies" is an array containing two string elements ["reading", "traveling"].
  • "address": The value with key "address" is a nested JSON object representing address information.

Use JSON

In programming, you can use different programming languages ​​to parse and generate JSON data.

  • Parse JSON: Use the built-in functions or libraries of the corresponding programming language to convert JSON strings into objects, arrays, and other data structures for manipulation.

    Example (using JavaScript):

    var jsonString = '{"name": "John Doe", "age": 30}';
    var jsonObject = JSON.parse(jsonString);
    console.log(jsonObject.name);  // 输出 "John Doe"
    
  • Generate JSON: Use the built-in functions or libraries of the corresponding programming language to convert data structures such as objects and arrays into JSON strings.

    Example (using JavaScript):

    var jsonObject = {
          
           "name": "John Doe", "age": 30 };
    var jsonString = JSON.stringify(jsonObject);
    console.log(jsonString);  // 输出 '{"name":"John Doe","age":30}'
    

Summarize

Congratulations! You've learned the basics of getting started quickly with the JSON data format. JSON provides a simple, flexible way to organize and exchange data. By mastering the basic data structure and usage of JSON, you can process data more efficiently during front-end and back-end data transmission and storage.

Guess you like

Origin blog.csdn.net/m0_53157282/article/details/133271280