• 理解JSON的语法


    JSON语法可以分为三种类型:

    简单值

    对象

    数组

    简单值:

    5
    "Hello World"

    JavaScript字符串与JSON字符串的最大区别在于,JSON字符串必须使用双引号

    对象:

    JSON中的对象与JavaScript字面量稍微有一些不同。下面是一个JavaScript中的对象字面量

    var person={
                name:"Qianwei",
                age:22
    }

    这虽然是开发人员在JavaScript中创建对象字面量的标准方式,但JSON中的对象要求给属性加引号。

    {
                "name":"Qianwei",
                "age":22
    }

    与JavaScript的对象字面量相比,JSON对象有两个地方不一样。首先,没有申明变量(JSOn中没有变量的概念)。其次,没有末尾的分号(因为这不是JavaScript语句,所以不需要分号)。再说一遍,对象的属性必须加引号,这在JSON中是必须的。属性的值可以是简单值,也已是复杂类型值。

    {
        "name": "Qianwei",
        "age": 22,
        "school": {
            "name": "anqing",
            "lOaction": "Anqing"
        }
    }

    这个例子在顶级对象中嵌入了学校信息。虽然有两个“name”属性,但是是不一样的。

    数组:

    JSON中的第二种复杂数据类型是数组。JSON数组采用的就是JavaScript中的数组字面量形式:

    var value = [25,"hi",true]

    在JSON中,可以采用同样的语法表示同一个数组:

    [{
            "title": "Pro",
            "author": "Qianwe",
            "edition": 3,
            "year": 2100
        },
        {
            "title": "Pro",
            "author": "Qianwe",
            "edition": 4,
            "year": 2100
        },
        {
            "title": "Pro",
            "author": "Qianwe",
            "edition": 2,
            "year": 2100
        }
    ]

    解析和序列化:

    可以把JSON数据结构解析为有用的JavaScript对象。

    [{
            "title": "Pro",
            "author": "Qianwe",
            "edition": 3,
            "year": 2100
        },
        {
            "title": "Pro1",
            "author": "Qianwe",
            "edition": 4,
            "year": 2100
        },
        {
            "title": "Pro2",
            "author": "Qianwe",
            "edition": 2,
            "year": 2100
        }
    ]
    
    ------------------------------------------------------------
    books[2].title                //pro2

    JSON对象:

    JSON对象有两个方法:stringify()和parse()。在最简单的情况下,这两个方法分别用于把JavaScript对象序列化为JSON字符串和把JSON字符串解析为原生的JavaScript值。

        var books = {
            "title": "Pro",
            "author": "Qianwe",
            "edition": 4,
            "year": 2100
        };
    
        var jsonText = JSON.stringify(books);
    
        console.log(jsonText)       //{"title":"Pro","author":"Qianwe","edition":4,"year":2100}
        console.log(typeof jsonText)        //string
    var books = {
            "title": "Pro",
            "author": "Qianwe",
            "edition": 4,
            "year": 2100
        };
    
        var jsonText = JSON.stringify(books);
    
        console.log(jsonText) //{"title":"Pro","author":"Qianwe","edition":4,"year":2100}
        console.log(typeof jsonText) //string
    
        var parseText = JSON.parse(jsonText);
        console.log(parseText)
            /*
            {title: "Pro", author: "Qianwe", edition: 4, year: 2100}
                author
                :
                "Qianwe"
                edition
                :
                4
                title
                :
                "Pro"
                year
                :
                2100
                __proto__
                :
                Object
            */
        console.log(typeof parseText) //object
  • 相关阅读:
    什么是 Spring 框架?Spring框架有哪些主要模块?
    java动态代理实现与原理详细分析
    react中 函数式组件hook的使用
    JavaScript中的delete的定义以及使用
    React中ref的使用
    使用react中antd design UI库from中的getFieldsValue,getFieldValue,validateFields,resetFields,getFieldDecorator,initialvalue的用法
    使用react和axios实现的城市三级联动
    关于react中antd design pro下面src/models
    关于thisState的那些事
    react的生命周期
  • 原文地址:https://www.cnblogs.com/QianBoy/p/8481180.html
Copyright © 2020-2023  润新知