【发布时间】:2020-02-16 17:59:40
【问题描述】:
我有这个 json 文件,我必须删除一些空字段。我已经发布了我的 json 文件以及我期待的 json 文件。你能帮我写什么代码来实现这一点吗?
这是我的 json 文件:
{
"selection1": [
{
"name": "Radisson Blu Azuri Resort & Spa",
"url": "https://www.marideal.mu/hotel-deals/radisson-blu-azuri-resort-hotel-deals.html",
"reviews": [
{
"name": "4",
"date": "October 09, 2015",
"review": "The personal was friendly , helpful n professional and put you at ease."
},
{
"name": "3",
"date": "July 20, 2015",
"review": "Everyone really enjoy each moment we had there. The staffs are all very friendly and professionals."
}
]
},
{
"name": "Manta Cove by Horizon Holidays",
"url": "https://www.marideal.mu/hotel-deals/manta-cove-by-horizon-holidays.html"
},
{
"name": "Sous Le Badamier",
"url": "https://www.marideal.mu/hotel-deals/sous-le-badamier.html"
},
{
"name": "Honeymoon Special at Lagoon Attitude",
"url": "https://www.marideal.mu/hotel-deals/honeymoon-special-at-lagoon-attitude.html",
"reviews": [
{
"name": "3",
"date": "January 20, 2020",
"review": "Lagoon Attitude has been my best hotel stay ever in Mauritius. The eco friendly concept is one of a kind, esp. with the water fountains. My friends and I were warmly welcomed upon arrival. The staff were amazingly friendly, very professional and hospitable."
},
{
"name": "134",
"date": "June 12, 2019",
"review": "Had an amazing stay for 2 nights.\nThe cleanliness of the room is faultless"
},
{
"name": "132",
"date": "April 11, 2019",
"review": "A highly recommendable hotel. Value for money and thanks to Marideal.\nThe room provided to us was very clean, and well maintained, we got a room in the 1800`s range, personal swimming pool along being cleaned everyday."
},
{
"name": "131",
"date": "January 24, 2019",
"review": "Super week-end en famille. Merci à toute l'équipe du Radisson pour votre accueil, votre gentilesse et votre écoute."
},
{
"name": "2",
"date": "January 06, 2020",
"review": "Our second visit to the hotel. Fantastic experience with the new Otentik concept. The personnel is the most welcoming and kind to us, namely Mr Kevin and Mr Jevissen, as well as Ms Veronique, to name a few."
}
]
}
]
}
我应该怎么做才能删除空字段并使我的 json 文件看起来像:
{
"selection1": [
{
"name": "Radisson Blu Azuri Resort & Spa",
"url": "https://www.marideal.mu/hotel-deals/radisson-blu-azuri-resort-hotel-deals.html",
"reviews": [
{
"name": "4",
"date": "October 09, 2015",
"review": "The personal was friendly , helpful n professional and put you at ease."
},
{
"name": "3",
"date": "July 20, 2015",
"review": "Everyone really enjoy each moment we had there. The staffs are all very friendly and professionals."
}
]
},
{
"name": "Honeymoon Special at Lagoon Attitude",
"url": "https://www.marideal.mu/hotel-deals/honeymoon-special-at-lagoon-attitude.html",
"reviews": [
{
"name": "3",
"date": "January 20, 2020",
"review": "Lagoon Attitude has been my best hotel stay ever in Mauritius. The eco friendly concept is one of a kind, esp. with the water fountains. My friends and I were warmly welcomed upon arrival. The staff were amazingly friendly, very professional and hospitable."
},
{
"name": "134",
"date": "June 12, 2019",
"review": "Had an amazing stay for 2 nights.\nThe cleanliness of the room is faultless"
},
{
"name": "132",
"date": "April 11, 2019",
"review": "A highly recommendable hotel. Value for money and thanks to Marideal.\nThe room provided to us was very clean, and well maintained, we got a room in the 1800`s range, personal swimming pool along being cleaned everyday."
},
{
"name": "131",
"date": "January 24, 2019",
"review": "Super week-end en famille. Merci à toute l'équipe du Radisson pour votre accueil, votre gentilesse et votre écoute."
},
{
"name": "2",
"date": "January 06, 2020",
"review": "Our second visit to the hotel. Fantastic experience with the new Otentik concept. The personnel is the most welcoming and kind to us, namely Mr Kevin and Mr Jevissen, as well as Ms Veronique, to name a few."
}
]
}
]
}
有人可以向我解释如何删除空字段,以便我的 json 文件准备好对每个循环的“审查”进行预处理吗?
【问题讨论】:
-
将 JSON 文件读入内存以获得字典,从字典中删除空元素,将字典保存回 JSON 文件
-
我很难做到这一点,你能帮帮我吗?
-
将
for-loop 与评论一起使用并复制到您要保留的其他列表元素 - 然后将其分配到旧列表的位置 -
你可以帮我把它写在答案框中吗?
-
到底是什么问题?你有没有尝试过,做过任何研究? Stack Overflow 不是免费的代码编写服务。请参阅:tour、How to Ask、help center、meta.stackoverflow.com/questions/261592/…。