gpt4 book ai didi

javascript - 如何使用 javascript 解析带有数组的外部 JSON 文件

转载 作者:搜寻专家 更新时间:2023-10-31 23:25:59 24 4
gpt4 key购买 nike

我目前正在努力解决使用 javascript 读取 JSON 文件的问题。
我不完全确定这是否是包含数组的 JSON 文件的正确格式,但这是我的 JSON 文件。

  [
{
"passageNumber":"2.3.1",
"title":"Inside and out: A bronze Athena and a Temple of Octavia",
"preReading":"This paragraph appears to refer to what the excavators named Temple E...",
"reading":"<span>Lorem</span> ipsum <span>dolor</span> sit amet, consectetur",
"media":"<img src='img/TempleE-capital.jpg'>",
"lon":"41.925",
"lat":"-91.426"
},
{
"passageNumber":"2.3.2",
"title":"The Road to Lechaeum",
"preReading":"<a href='http://google.com'>yipppie",
"postReading":"",
"reading":"blahhhhhhhhhhhhhhhhhh.",
"media":"<img src='img/templE-brick.jpg'>",
"lon":"41.625",
"lat":"-91.672"
}
]

我最终希望能够读取 JSON 文件(最有可能使用 JQuery),然后选择给定段落编号的所有信息。
任何帮助都会很棒。
谢谢你!
编辑我是从外部 JSON 文件中提取的。它需要加载到 JSON 文件中。

最佳答案

下面是如何读取 JSON 的示例片段。

var JSONDataFromExternalFile = '[{"passageNumber":"2.3.1","title":"Inside and out: A bronze Athena and a Temple of Octavia","preReading":"This paragraph appears to refer to what the excavators named Temple E...","reading":"<span>Lorem</span> ipsum <span>dolor</span> sit amet, consectetur","media":"<img src=\'img/TempleE-capital.jpg\'>","lon":"41.925","lat":"-91.426"},{"passageNumber":"2.3.2","title":"The Road to Lechaeum","preReading":"<a href=\'http://google.com\'>yipppie","postReading":"","reading":"blahhhhhhhhhhhhhhhhhh.","media":"<img src=\'img/templE-brick.jpg\'>","lon":"41.625","lat":"-91.672"}]'

var data = JSON.parse(JSONDataFromExternalFile);

function getDetails(passageNumber){

for(i in data){
if (data[i].passageNumber == passageNumber)
return data[i];
}
return false;
}

var details = getDetails("2.3.2");
alert("title > "+details.title);
alert("preReading > "+details.preReading);

var details = getDetails("2.3.1");
alert("title > "+details.title);
alert("preReading > "+details.preReading);

在您的代码中,它可能看起来像这样。

更新

$.ajax({
url: "http://www.json-generator.com/api/json/get/cgRivNixNK",
type: "POST", //type:"GET"
dataType: "JSON",
success: function(data){
console.log(data)
}
})

$.ajax({
url: "http://www.json-generator.com/api/json/get/cgRivNixNK",
type: "POST", //type:"GET"
dataType: "JSON"
})
.done(function(data){
console.log(data)
});

关于javascript - 如何使用 javascript 解析带有数组的外部 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31103502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com