zoukankan      html  css  js  c++  java
  • json格式的转换为json字符串函数

    function toJSON(object){
    var type = typeof object;
    if ('object' == type) {
    if (Array == object.constructor)
    type = 'array';
    else if (RegExp == object.constructor)
    type = 'regexp';
    else
    type = 'object';
    }
    switch (type) {
    case 'undefined':
    case 'unknown':
    return;
    case 'function':
    case 'boolean':
    case 'regexp':
    return object.toString();
    case 'number':
    return isFinite(object) ? object.toString() : 'null';
    case 'string':
    return '"' + object.replace(/(|")/g, "$1").replace(/n|r|t/g, function(){
    var a = arguments[0];
    return (a == 'n') ? 'n': (a == 'r') ? 'r': (a == 't') ? 't': ""
    }) + '"';
    case 'object':
    if (object === null)
    return 'null';
    var results = [];
    for (var property in object) {
    var value = toJSON(object[property]);
    if (value !== undefined) results.push(toJSON(property) + ':' + value);
    }
    return '{' + results.join(',') + '}';
    case 'array':
    var results = [];
    for (var i = 0; i < object.length; i++) {
    var value = toJSON(object[i]);
    if (value !== undefined) results.push(value);
    }
    return '[' + results.join(',') + ']';
    }
    }

  • 相关阅读:
    android数据恢复
    UVA 690 Pipeline Scheduling
    2017 国庆湖南 Day4
    2017 国庆湖南 Day5
    2017 国庆湖南 Day6
    2017国庆 清北学堂 北京综合强化班 Day1
    2017 国庆湖南Day2
    bzoj 2962 序列操作
    UVA 818 Cutting Chains
    UVA 211 The Domino Effect
  • 原文地址:https://www.cnblogs.com/rainbow661314/p/3227077.html
Copyright © 2011-2022 走看看