Json object To String 函数

原创 2011年01月14日 14:52:00

       function json2string(strObject) {
            var c, i, l, s = '', v, p;
            switch (typeof strObject) {
                case 'object':
                    if (strObject) {
                        if (strObject.length && typeof strObject.length == 'number') {
                            for (i = 0; i < strObject.length; ++i) {
                                v = json2string(strObject[i]);
                                if (s) {
                                    s += ',';
                                }
                                s += v;
                            }
                            return '[' + s + ']';
                        } else if (typeof strObject.toString != 'undefined') {
                            for (i in strObject) {
                                v = strObject[i];
                                if (typeof v != 'undefined' && typeof v != 'function') {
                                    v = json2string(v);
                                    if (s) {
                                        s += ',';
                                    }
                                    s += json2string(i) + ':' + v;
                                }
                            }
                            return '{' + s + '}';
                        }
                    }
                    return 'null';
                case 'number':
                    return isFinite(strObject) ? String(strObject) : 'null'; case 'string': l = strObject.length; s = '"';
                    for (i = 0; i < l; i += 1) {
                        c = strObject.charAt(i);
                        if (c >= ' ') {
                            if (c == '//' || c == '"') {
                                s += '//';
                            }
                            s += c;
                        } else {
                            switch (c) {
                                case '/b': s += '//b'; break;
                                case '/f': s += '//f'; break;
                                case '/n': s += '//n'; break;
                                case '/r': s += '//r'; break;
                                case '/t': s += '//t'; break;
                                default: c = c.charCodeAt(); s += '//u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
                            }
                        }
                    }
                    return s + '"';
                case 'boolean': return String(strObject);
                default: return 'null';
            }
        }

相关文章推荐

Object(String,list,object and so on) To Json

package com.whaugmentum.training.util; import java.beans.IntrospectionException; import java.beans...

黑马程序员-java垃圾回收原理、组合设计模式、Object中的两个方法(to String() finalize())总结

---------------------- android培训、java培训、期待与您交流! ---------------------- 1、  垃圾回收: (1)       对象销...

iOS开发 AFNetworking 关于JSON text did not start with array or object and option to allow fragments not

AFNetworking 关于JSON text did not start with array or object and option to allow fragments not set 错误...

简单封装Jackson,实现JSON String到Java Object的Mapper.

package com.zcgt.shop.common.mapper;import java.io.IOException; import java.util.Collection; impor...

Json , String, Object , List 之间的相互转换

首先定义了一个Filter类package com.xin.map.bean; public class Filter { private String name; private Strin...

一个实用的 Javascript XML to JSON Object 对象的转换 (JQuery)

一个实用的 Javascript XML to JSON  Object 对象的转换 (JQuery) 一个用来将xml文件转换成对象的插件 xml文件如下   TreeVi...

Java object 和 json string之间的相互转换

开发环境 Jackson的相关jar包(jackson-databind,jackson-core,jackson-annotation和json) junit的jar包,建议使用4.0以后版本 ...
  • wlyynl
  • wlyynl
  • 2015年12月01日 11:11
  • 292

原:PHP数组函数研究:is_null,is_object,is_array,is_string,is_resource等

声明:本文为斯人原创,全部为作者一一分析得之,有不对的地方望赐教。 欢迎转载,转载请注明出处 。 本文地址:http://imsiren.com/archives/516 前面有一篇...

Java JSON - Convert Java Object To / From JSON (Jackson)

Java JSON - Convert Java Object To / From JSON (Jackson) JSON(JavaScript Object Notation)是一种轻量级的数...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Json object To String 函数
举报原因:
原因补充:

(最多只允许输入30个字)