json对象转换成string的方法


jquery打印某个json对象的方法:
<script>
var jsonToString=typeof JSON !="undefined"? JSON.stringify :function(obj){
  var arr =[];
  $.each(obj,function(key, val){
    var next = key +": ";
    next += $.isPlainObject(val)? jsonToString(val): val;
    arr.push( next );
  });
  return"{ "+  arr.join(", ")+" }";
};
var jsonstring = {
                "modeldata" : [ {
                    "chart.data" : 12,
                    "chart.colors" : "red",
                    "chart.labels" : "John(12%)",
                    "chart.exploded" : 10
                }, {
                    "chart.data" : 18,
                    "chart.colors" : "blue",
                    "chart.labels" : "Richard(18%)",
                    "chart.exploded" : 20
                }, {
                    "chart.data" : 25,
                    "chart.colors" : "green",
                    "chart.labels" : "James(25%)",
                    "chart.exploded" : 20
                }, {
                    "chart.data" : 45,
                    "chart.colors" : "pink",
                    "chart.labels" : "Green(45%)",
                    "chart.exploded" : 20
                } ],
                "operate" : [ "hello" ]
            };
            alert(JSON.jsonToString(jsonstring));
 
</script>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM