zoukankan      html  css  js  c++  java
  • Json操作

    JSON操作

    var book = {
    
                    title: "js",
    
                    authors: ["zhang"],
    
                    edition: "3",
    
                    year: 2011,
    
                    test:undefined
    
                };
    
                var jsonText = JSON.stringify(book);//{"title":"js","authors":["zhang"],"edition":"3","year":2011}
    
                console.log(jsonText);
    
     
    
                var json = JSON.parse(jsonText);
    
                console.log(json.title);//js

    在序列化JavaScript对象时,所有函数及原型成员会被忽略,另外,值为undefined的属性也会被忽略。

    JSON.stringify()

    该方法可以传递两个参数,第一个参数是过滤器,可以是一个数组或方法,第二个参数是一个选项,表示是否在json字符串中保留缩进。

    传递数组

     var jsonText = JSON.stringify(book, ["title", "year"]);//{"title":"js","year":2011}

    传递方法

    var jsonText = JSON.stringify(book, function (key,value) {
    
                    switch (key) {
    
                        case "authors":
    
                            return value.join(",");
    
                        case "year":
    
                            return 5000;
    
                        case "edition":
    
                            return undefined;
    
                        default:
    
                            return value;
    
                    }
    
                });//{"title":"js","authors":"zhang","year":5000}

    注意一定要提供default项,以便其他值能正常出现在结果中。

    字符串缩进

    var jsonText = JSON.stringify(book, null,4);//格式缩进
    
    {
    
        "title": "js",
    
        "authors": [
    
            "zhang"
    
        ],
    
        "edition": "3",
    
        "year": 2011
    
    }
    var jsonText = JSON.stringify(book, null,"- -");//格式缩进
    
    {
    
    - -"title": "js",
    
    - -"authors": [
    
    - -- -"zhang"
    
    - -],
    
    - -"edition": "3",
    
    - -"year": 2011
    
    }

    Json.Parse()

    该方法也会接收一个参数,为一个function,表示还原函数进行操作。

    var book = {
    
                    title: "js",
    
                    authors: ["zhang"],
    
                    edition: "3",
    
                    year: 2011,
    
                    test: undefined,
    
                    replaseDate:new Date(2011,11,1)
    
                };
    
                var jsonText = JSON.stringify(book);//{"title":"js","authors":["zhang"],"edition":"3","year":2011,"replaseDate":"2011-11-30T16:00:00.000Z"}
    
                console.log(jsonText);
    
     
    
                var json = JSON.parse(jsonText, function (key, value) {
    
                    if (key == "replaseDate") {
    
                        return new Date(value);
    
                    }
    
                    else {
    
                        return value;
    
                    }
    
                });
    
                console.log(json.replaseDate);//Thu Dec 01 2011 00:00:00 GMT+0800 (中国标准时间)
  • 相关阅读:
    禅道,导出选中的用例
    chrome 历史版本下载地址
    在QT中添加zeromq DLL库
    在QT中添加zeromq DLL库
    在QT中添加zeromq库,zeromq的下载编译
    工控软件dll劫持漏洞分析
    [转载]Impost3r:一款针对Linux的密码提取工具
    0MQ绑定Delphi版-说明
    通过Maven导出war包时报错:Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project Ocr: Error assembling WAR: webxml
    价值投资必须掌握的几个指标zz
  • 原文地址:https://www.cnblogs.com/y8932809/p/5429746.html
Copyright © 2011-2022 走看看