需要一些关于在客户端操作JSON数据的建议。
有关如何在JS / jQuery中将TYPE1转换为TYPE2的任何提示?
转换(TYPE1):
{
"0": {
"Field1": "Val1",
"Field2": "Val2",
"Field3": "Val3"
},
"1": {
"Field1": "Val1",
"Field2": "Val2",
"Field3": "Val3"
}
}
进入此(TYPE2):
{
"SomeName": [
[
"Val1",
"Val3",
"Val3"],
[
"Val4",
"Val5",
"Val6"]
]
}
答案 0 :(得分:0)
试试这个:
var oldObj = {
"0": {
"Field1": "Val1",
"Field2": "Val2",
"Field3": "Val3"
},
"1": {
"Field1": "Val1",
"Field2": "Val2",
"Field3": "Val3"
}
}
var newObj = {"SomeName":[]}; // Initialize a new Object
for(var key in oldObj){ // Loop through the old item,
var item = []; // Create a temporary variable for each sub-item.
for(var subKey in oldObj[key]){ // Loop through the sub-item
item.push(oldObj[key][subKey]); // Push the sub-item's data to the temp var.
}
newObj.SomeName.push(item); // Push the temp var to the new Object.
}