我有一个 JSON 对象,在一些操作(例如删除某些部分)之后,我将其打印出来,除了有一些 null
值,一切看起来都很好。我该如何移除这些值?
我使用 JSON.stringify(obj, null, 2)
方法进行打印,并且以下是它的样子:
{
"store": {
"book": [
null,
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
null,
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
null,
"price": 19.95
}
}
}
我希望它更加简洁,非常干净(删除额外的3个null
值):
{
"store": {
"book": [
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
null
,你的“bicycle”语法无效。 - Joe EnosJSON.stringify
的实现生成了无效的JSON。 - James M