json序列化

Posted 编程杂技

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json序列化相关的知识,希望对你有一定的参考价值。

    var isObject = function(obj{
        return Object.prototype.toString.call(obj) === '[object Object]';
    }

    var isArray  = function(arr{
        return Object.prototype.toString.call(arr) === '[object Array]';
    }


function arrSerialize(arr{
    var res = [];
    if (isObject(arr)) {
        return;
    }
    if (isArray(arr)) {
        for (var i = 0;i<arr.length;i++) {
            if (isArray(arr[i])) {
                res.push(arrSerialize(arr[i]));
            } else {
                res.push(objSerialize(arr[i]));
            }

        }
        return "["+res.join(',')+"]"
    } else {
        if (typeof arr === 'string') {
            return '"'+arr+'"';
        } else if (typeof arr === 'boolean' || arr === null || typeof arr === 'number'){
            return arr;
        } else {
            throw new Error('arguments error');
        }
    }

}

function objSerialize(obj{
    var res = [];
    if (isArray(obj)) {
        return;
    }
    if (isObject(obj)) {
        for (var key in obj) {
            if (isObject(obj[key])) {
                res.push('"'+key+'"'+":"+objSerialize(obj[key]));
            } else {
                res.push('"'+key+'"'+":"+arrSerialize(obj[key]));
            }

        }
        return "{"+res.join(',')+"}"
    } else {
        if (typeof obj === 'string') {
            return '"'+obj+'"';
        } else if (typeof obj === 'boolean' || obj === null || typeof obj === 'number'){
            return obj;
        } else {
            throw new Error('arguments error');
        }
    }

}
MYJSON = {
    stringifyfunction(arg{
        if (isObject(arg)) {
            return objSerialize(arg);
        } else if (isArray(arg)){
            return arrSerialize(arg)
        }
    }
}
console.log(MYJSON.stringify({
    "dependencies": {
        "JSONStream""~1.3.1",
        "abbrev""~1.1.1",
        "ansi-regex""~3.0.0",
        "ansicolors""~0.3.2",
        "ansistyles""~0.1.3",
        "aproba""~1.2.0",
        "archy""~1.0.0",
        "bin-links""^1.1.0",
        "bluebird""~3.5.1",
        "cacache""~10.0.1",
        "call-limit""~1.1.0",
        "chownr""~1.0.1",
        "cli-table2""~0.2.0",
        "cmd-shim""~2.0.2",
        "columnify""~1.5.4",
        "config-chain""~1.1.11",
        "detect-indent""~5.0.0",
        "dezalgo""~1.0.3",
        "editor""~1.0.0",
        "find-npm-prefix""~1.0.1",
        "fs-vacuum""~1.2.10",
        "fs-write-stream-atomic""~1.0.10",
        "gentle-fs""^2.0.1",
        "glob""~7.1.2",
        "graceful-fs""~4.1.11",
        "has-unicode""~2.0.1",
        "hosted-git-info""~2.5.0",
        "iferr""~0.1.5",
        "inflight""~1.0.6",
        "inherits""~2.0.3",
        "ini""~1.3.4",
        "init-package-json""~1.10.1",
        "is-cidr""~1.0.0",
        "lazy-property""~1.0.0",
        "libnpx""~9.7.1",
        "lockfile""~1.0.3",
        "lodash._baseuniq""~4.6.0",
        "lodash.clonedeep""~4.5.0",
        "lodash.union""~4.6.0",
        "lodash.uniq""~4.5.0",
        "lodash.without""~4.4.0",
        "lru-cache""~4.1.1",
        "meant""~1.0.1",
        "mississippi""~1.3.0",
        "mkdirp""~0.5.1",
        "move-concurrently""^1.0.1",
        "nopt""~4.0.1",
        "normalize-package-data""~2.4.0",
        "npm-cache-filename""~1.0.2",
        "npm-install-checks""~3.0.0",
        "npm-lifecycle""~2.0.0",
        "npm-package-arg""~6.0.0",
        "npm-packlist""~1.1.10",
        "npm-profile""~2.0.5",
        "npm-registry-client""~8.5.0",
        "npm-user-validate""~1.0.0",
        "npmlog""~4.1.2",
        "once""~1.4.0",
        "opener""~1.4.3",
        "osenv""~0.1.4",
        "pacote""^7.0.2",
        "path-is-inside""~1.0.2",
        "promise-inflight""~1.0.1",
        "qrcode-terminal""~0.11.0",
        "query-string""~5.0.1",
        "qw""~1.0.1",
        "read""~1.0.7",
        "read-cmd-shim""~1.0.1",
        "read-installed""~4.0.3",
        "read-package-json""~2.0.12",
        "read-package-tree""~5.1.6",
        "readable-stream""~2.3.3",
        "request""~2.83.0",
        "retry""~0.10.1",
        "rimraf""~2.6.2",
        "safe-buffer""~5.1.1",
        "semver""~5.4.1",
        "sha""~2.0.1",
        "slide""~1.1.6",
        "sorted-object""~2.0.1",
        "sorted-union-stream""~2.1.3",
        "ssri""~5.0.0",
        "strip-ansi""~4.0.0",
        "tar""^4.0.2",
        "text-table""~0.2.0",
        "uid-number""0.0.6",
        "umask""~1.1.0",
        "unique-filename""~1.1.0",
        "unpipe""~1.0.0",
        "update-notifier""~2.3.0",
        "uuid""~3.1.0",
        "validate-npm-package-name""~3.0.0",
        "which""~1.3.0",
        "worker-farm""~1.5.1",
        "wrappy""~1.0.2",
        "write-file-atomic""~2.1.0"
      },
      "Image": {
          "Width":  800,
          "Height"600,
          "Title":  "View from 15th Floor",
          "Thumbnail": {
              "Url":    "http://www.example.com/image/481989943",
              "Height"125,
              "Width":  "100"
          },
          "IDs": [11694323438793]}})
)


以上是关于json序列化的主要内容,如果未能解决你的问题,请参考以下文章

json 个人的vscode的代码片段

RestFramework

实用代码片段将json数据绑定到html元素 (转)

json 可视代码工作室Angular with Firebase片段

vs code 用户代码片段 html.json

错误代码:错误域 = NSCocoaErrorDomain 代码 = 3840“JSON 文本没有以数组或对象和允许未设置片段的选项开头。”