欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

json相关操作

程序员文章站 2024-03-18 18:24:40
...

1.json对象转换为数组

var fields={
   "CeShi": "测试",
   "CheXingYaoQiu": "车型要求",
   "ChengCheRenShu": "乘车人数",
   "ChuFaDiDian": "出发地点",
   "ErJiShenPi": "二级审批",
   "FeiYongChengDan": "费用承担",
   "LianXiDianHua": "联系电话"
   
  }
  //转换为[{},{},{}]形式
  var sx=[];
  Object.keys(fields).forEach(v => {
   let o = {};
   o[v] = fields[v];
   sx.push(o)
  })
  alert(JSON.stringify(sx))
//输出结果
//[{"CeShi":"测试"},{"CheXingYaoQiu":"车型要求"},{"ChengCheRenShu":"乘车人数"},{"ChuFaDiDian":"出发地点"},{"ErJiShenPi":"二级审批"},{"FeiYongChengDan":"费用承担"},{"LianXiDianHua":"联系电话"}]

2.json数组删除某个值

var  s = [{"name":"tank","total":"100"},{"name":"zhang","total":"23"},{"name":"hao","total":"325"}];
for(var k=0;k<s.length;k++){
    delete s[k]["name"];
    }
alert(JSON.stringify(s))
//输出结果
//[{"total":"100"},{"total":"23"},{"total":"325"}]