我正在做一些运动,例如:
var jsonres;
jsonres = JSON.stringify(jsonObjectArray);
alert(jsonvals); // getting the below json structure
jsonres = {
"key01": [10, "Key01 Description"],
"key02": [false, "It's a false value"],
"key03": [null, "Testing Null"],
"key04": ["tests", "Another Test Value"],
"key05": [[25, 50], "Some testing values"]
}
但我需要这样:
jsonres = {
"key01": 10,
"key02": false,
"key03": null,
"key04": "tests",
"key05": [25,50]
}
我怎么能像上面的结构(意味着我只需要单个值,不需要各个键的第二个值/多个值)?请帮助我,在此先感谢。
最佳答案
var jsonres = {
"key01": [10, "Key01 Description"],
"key02": [false, "It's a false value"],
"key03": [null, "Testing Null"],
"key04": ["tests", "Another Test Value"],
"key05": [[25, 50], "Some testing values"]
}
for(var key in jsonres){
if(jsonres.hasOwnProperty(key)){
jsonres[key] = jsonres[key][0];
}
}
console.log(jsonres)
https://jsfiddle.net/xd4nwc0m/