Javascript 根据特定标准重新设置json文件内容的范围
我在谷歌电子表格中有一些数据。我使用php脚本将.csv文件转换为.json文件 我的结果如下所示:Javascript 根据特定标准重新设置json文件内容的范围,javascript,php,json,Javascript,Php,Json,我在谷歌电子表格中有一些数据。我使用php脚本将.csv文件转换为.json文件 我的结果如下所示: "Destination ": "Fuerteventura", "Hotel_name": "Aloe Club", "USP": "Børnevenligt;;Lejligheder op til 6 personer;;Tre dejlige poolområder ", "Fra-pris": "1234", "Se_flere_rejser_UR
"Destination ": "Fuerteventura",
"Hotel_name": "Aloe Club",
"USP": "Børnevenligt;;Lejligheder op til 6 personer;;Tre dejlige poolområder ",
"Fra-pris": "1234",
"Se_flere_rejser_URL": "http:\/\/www.spies.dk\/vinterferie",
"Læs_mere_her_URL ": "http:\/\/www.spies.dk\/de-kanariske-oer\/corralejo\/hoteller",
"Image_url": "https:\/\/spies-isobar.s3.amazonaws.com\/1355_Spies_AVO\/cp\/destination\/a\/a\/image1.jpg",
"id": 0
},
{
"Destination ": "Fuerteventura",
"Hotel_name": "SunConnect Atlantis Fuerteventura Resort",
"USP": "Børnevenligt;;All Inclusive indgår;;Mange aktiviteter for hele familien ",
"Fra-pris": "123",
"Se_flere_rejser_URL": "http:\/\/www.spies.dk\/vinterferie",
"Læs_mere_her_URL ": "http:\/\/www.spies.dk\/de-kanariske-oer\/corralejo\/hoteller",
"Image_url": "https:\/\/spies-isobar.s3.amazonaws.com\/1355_Spies_AVO\/cp\/destination\/a\/a\/image1.jpg",
"id": 1
},
我想知道以这种方式重新设置内容物范围的最佳方式是什么,以便Fuertventura的所有酒店都是fuertaventura目的地的孩子。像这样的
"Destination ": "Fuerteventura"{
"Hotel_name": "Aloe Club",
"USP": "Børnevenligt;;Lejligheder op til 6 personer;;Tre dejlige poolområder ",
"Fra-pris": "1234",
"Se_flere_rejser_URL": "http:\/\/www.spies.dk\/vinterferie",
"Læs_mere_her_URL ": "http:\/\/www.spies.dk\/de-kanariske-oer\/corralejo\/hoteller",
"Image_url": "https:\/\/spies-isobar.s3.amazonaws.com\/1355_Spies_AVO\/cp\/destination\/a\/a\/image1.jpg",
"id": 0,
"Hotel_name": "SunConnect Atlantis Fuerteventura Resort",
"USP": "Børnevenligt;;All Inclusive indgår;;Mange aktiviteter for hele familien ",
"Fra-pris": "123",
"Se_flere_rejser_URL": "http:\/\/www.spies.dk\/vinterferie",
"Læs_mere_her_URL ": "http:\/\/www.spies.dk\/de-kanariske-oer\/corralejo\/hoteller",
"Image_url": "https:\/\/spies-isobar.s3.amazonaws.com\/1355_Spies_AVO\/cp\/destination\/a\/a\/image1.jpg",
"id": 1
},
我猜可能一些JavaScript代码会起作用,但我不知道从哪里开始。我想您希望json以这种方式排序,因为json对象中不能有两个同名属性(例如:您的酒店id) 您在json中读取的数据应该在如下数组中:
[{
"Destination ": "Fuerteventura",
"Hotel_name": "Aloe Club",
"USP": "Børnevenligt;;Lejligheder op til 6 personer;;Tre dejlige poolområder ",
"Fra-pris": "1234",
"Se_flere_rejser_URL": "http:\/\/www.spies.dk\/vinterferie",
"Læs_mere_her_URL ": "http:\/\/www.spies.dk\/de-kanariske-oer\/corralejo\/hoteller",
"Image_url": "https:\/\/spies-isobar.s3.amazonaws.com\/1355_Spies_AVO\/cp\/destination\/a\/a\/image1.jpg",
"id": 0
},
{
"Destination ": "Fuerteventura",
"Hotel_name": "SunConnect Atlantis Fuerteventura Resort",
"USP": "Børnevenligt;;All Inclusive indgår;;Mange aktiviteter for hele familien ",
"Fra-pris": "123",
"Se_flere_rejser_URL": "http:\/\/www.spies.dk\/vinterferie",
"Læs_mere_her_URL ": "http:\/\/www.spies.dk\/de-kanariske-oer\/corralejo\/hoteller",
"Image_url": "https:\/\/spies-isobar.s3.amazonaws.com\/1355_Spies_AVO\/cp\/destination\/a\/a\/image1.jpg",
"id": 1
}]
转换json的javascript可能如下代码所示:
var yourFirstJson; //contains your unordered json object
var yourSecondJson; //contains ordered json
//loop through all destination of your input json
for(var destination in yourFirstJson) {
//Name of your destination, will be the new json attribute
var destinationName = destination.Destination;
//delete the "Destination" name attribute what is still in your added object (i think you don't want it in your new json)
delete destination.Destination;
//add the destination as attribute of your second object or add an hotel to the array in this attribute
yourSecondJson[destinationName].push(destination);
}
我希望这能帮助您……我想您希望json以这种方式排序,因为json对象中不能有两个同名属性(例如:您的酒店id) 您在json中读取的数据应该在如下数组中:
[{
"Destination ": "Fuerteventura",
"Hotel_name": "Aloe Club",
"USP": "Børnevenligt;;Lejligheder op til 6 personer;;Tre dejlige poolområder ",
"Fra-pris": "1234",
"Se_flere_rejser_URL": "http:\/\/www.spies.dk\/vinterferie",
"Læs_mere_her_URL ": "http:\/\/www.spies.dk\/de-kanariske-oer\/corralejo\/hoteller",
"Image_url": "https:\/\/spies-isobar.s3.amazonaws.com\/1355_Spies_AVO\/cp\/destination\/a\/a\/image1.jpg",
"id": 0
},
{
"Destination ": "Fuerteventura",
"Hotel_name": "SunConnect Atlantis Fuerteventura Resort",
"USP": "Børnevenligt;;All Inclusive indgår;;Mange aktiviteter for hele familien ",
"Fra-pris": "123",
"Se_flere_rejser_URL": "http:\/\/www.spies.dk\/vinterferie",
"Læs_mere_her_URL ": "http:\/\/www.spies.dk\/de-kanariske-oer\/corralejo\/hoteller",
"Image_url": "https:\/\/spies-isobar.s3.amazonaws.com\/1355_Spies_AVO\/cp\/destination\/a\/a\/image1.jpg",
"id": 1
}]
转换json的javascript可能如下代码所示:
var yourFirstJson; //contains your unordered json object
var yourSecondJson; //contains ordered json
//loop through all destination of your input json
for(var destination in yourFirstJson) {
//Name of your destination, will be the new json attribute
var destinationName = destination.Destination;
//delete the "Destination" name attribute what is still in your added object (i think you don't want it in your new json)
delete destination.Destination;
//add the destination as attribute of your second object or add an hotel to the array in this attribute
yourSecondJson[destinationName].push(destination);
}
我希望这能帮助您……我不知道JavaScript是如何在这里发挥作用的。是否要在数据发送到客户端后重新构造数据?如果是这样,这与JSON、文本文件或PHP无关。您只是询问如何迭代数组并创建具有该结构的对象。或者您想在文件中存储这样的数据吗?是的,我希望数据以这种方式保存到文件中,然后更新您的PHP脚本以生成该结构?我不知道JavaScript是如何在这里发挥作用的。是否要在数据发送到客户端后重新构造数据?如果是这样,这与JSON、文本文件或PHP无关。您只是询问如何迭代数组并创建具有该结构的对象。或者您想在文件中存储这样的数据吗?是的,我想以这种方式将数据保存到文件中,然后更新PHP脚本以生成该结构?