在javascript中重命名JSON密钥

时间:2018-06-05 11:45:08

标签: javascript json react-native

你好朋友我想重命名我的JSON密钥,所以在我的代码下面



botocore




我想用数据键更改 WatchListItem 键但是当我在上面运行代码时,JSON键没有替换任何想法我该怎么解决这个问题?

3 个答案:

答案 0 :(得分:1)

objundefined。将obj更改为obj[0]修正了它。



var json = '[{"Status":"Success","Data":[{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"31","WishListName":"General","WishListItems":[{"WishListItemID":"3","ItemCode":"4414082000005","ItemName":"Notebook 4414-082","Image":"http://aljeel.gct.om/ProductsImages/4414082000005_MT1.jpg","ItemPrice":0.500,"CreatedDate":"25/05/2018"}]},{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"24","WishListName":"Default","WishListItems":[]}],"Message":null}]';

var obj = JSON.parse(json)[0].Data[0];

obj.data = obj.WishListItems;
delete obj.WishListItems;

json = JSON.stringify([obj]);
console.log("FINAL JSON " + (json));




答案 1 :(得分:1)

试试这个:

var json = '[{"Status":"Success","Data":[{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"31","WishListName":"General","WishListItems":[{"WishListItemID":"3","ItemCode":"4414082000005","ItemName":"Notebook 4414-082","Image":"http://aljeel.gct.om/ProductsImages/4414082000005_MT1.jpg","ItemPrice":0.500,"CreatedDate":"25/05/2018"}]},{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"24","WishListName":"Default","WishListItems":[]}],"Message":null}]';

var obj = JSON.parse(json)[0].Data;

console.log("Before replace", obj);

var res = obj.map(item => {
  item.data = item.WishListItems;
  delete item.WishListItems;
  return item;
});

console.log("After replace", res);

答案 2 :(得分:0)

你的代码的问题是你的obj不是一个对象它是一个数组。你必须使用

SELECT count(chr.Regulations_idRegulations)
       /
       count(r.idRegulations) level_of_compliance
       FROM Regulations r
            LEFT JOIN Companies_has_Regulations chr
                      ON chr.Regulations_idRegulations = r.idRegulations
       WHERE chr.Companies_idCompanies = <ID for the company in question>;
在分配中。 或者为所有对象

执行此操作

使用forEach,如下所示

<ID for the company in question>