日期:2014-05-16  浏览次数:20424 次

json转string方法;string转json;
//json转string
function jsonToString (obj){   
        var THIS = this;    
        switch(typeof(obj)){   
            case 'string':   
                return '"' + obj.replace(/(["\\])/g, '\\$1') + '"';   
            case 'array':   
                return '[' + obj.map(THIS.jsonToString).join(',') + ']';   
            case 'object':   
                 if(obj instanceof Array){   
                    var strArr = [];   
                    var len = obj.length;   
                    for(var i=0; i<len; i++){   
                        strArr.push(THIS.jsonToString(obj[i]));   
                    }   
                    return '[' + strArr.join(',') + ']';   
                }else if(obj==null){   
                    return 'null';   
  
                }else{   
                    var string = [];   
                    for (var property in obj) string.push(THIS.jsonToString(property) + ':' + THIS.jsonToString(obj[property]));   
                    return '{' + string.join(',') + '}';   
                }   
            case 'number':   
                return obj;   
            case false:   
                return obj;   
        }   
    }

===========================================================
String arr=new String(request.getParameter("arr").getBytes("ISO8859-1"), "UTF-8");
var arr2='<%=arr%>';
		var list=eval('('+arr2+')');//用eval函数将json转化为对象  
		alert(list[0].fileName);
		alert(list[1].fileName);
		alert(list[2].fileName);