将有效负载中的键值对转换为json对象

时间:2013-04-05 19:20:01

标签: jquery json

这是我的json有效载荷

"multiList": [
  {
    "Key": "my_key",
    "Value": this is my key
  },
  {
    "Key": "my_text_box",
    "Value": "This is my text box"
  },

如何使用jquery

动态转换它
"multiList": [
  {
    "my_key" : "this is my key"
  },
  {
    "my_text_box": "This is my text box"
  },

1 个答案:

答案 0 :(得分:0)

例如:

yourobj.multiList = yourobj.multiList.map(function(v) {
   var r = {};
   r[v.Key] = v.Value;
   return r;
});

如果您想支持IE8,则必须use a shim for map或使用for代替map

for (var i=0; i<yourobj.multiList.length; i++) {
    var v = yourobj.multiList[i];
    var r = {};
    r[v.Key] = v.Value;
    yourobj.multiList[i] = r;
}