在js中把json中的 key去掉双引号的方法


方法一:

      //数据格式是这样的:

      var data = '[{"id":30348079,"name":"表1","score":"1","pid":"0"},{"id":30348079,"name":"表1","score":"1","pid":"0"}]';

       var  jsonData = JSON.stringify(data).replace(/\ "/g, " ");  //这里去掉所有"
       console.log( "---" +jsonData);
         // 下边操作
         //  :替换:"
         //  ,替换",
         //  }]替换"}]
         //  }",{替换 "},{
       jsonData = jsonData.replace(/\:/g,"\:\ "" ).replace(/\,/g, "\"\," ).replace(/\}\]/g, "\"\}\]" ).replace(/\}\ "\,\{/g," \ "\}\,\{" );
       console.log( "++++" +jsonData);
         //最后转回json对象
       data = eval( '('  + jsonData +  ')' );
       console.log(data);
方法二:
  // 待处理的json对象 
var  jsonObj = { "title" : "\u660c\u5409" , "content" : "" , "point" : "87.997349|43.732302" , "isOpen" : "0" , "icon" : "w:21,h:21,l:0,t:0,x:6,lb:5" };         
// json的值将被临时储存在这个变量中
var  keyValue =  "" ;
// 处理好的json字符串
var  jsonStr =  "" ;
for  ( var  key  in  jsonObj) {
     keyValue += key +  ':"'  + jsonObj[key] +  '",' ;
}
// 去除最后一个逗号
keyValue = keyValue.substring(0,keyValue.length - 1);
jsonStr =  "{"  + keyValue +  "}" ;
console.log(jsonStr);
 


免责声明!

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



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