Jquery 从JSON数组中检索数组值
我正在尝试检索JSON数组中的值。但是,我的代码无法执行它。下面是该文件的外观Jquery 从JSON数组中检索数组值,jquery,arrays,json,ajax,Jquery,Arrays,Json,Ajax,我正在尝试检索JSON数组中的值。但是,我的代码无法执行它。下面是该文件的外观 { "meta" : { "view" : { "description" : "This data set shows the location of Baltimore City's Services.", "columns" : [{ "cachedContents" : { "top" : [{ "
{
"meta" : {
"view" : {
"description" : "This data set shows the location of Baltimore City's Services.",
"columns" : [{
"cachedContents" : {
"top" : [{
"item" : "American Rescue Workers"
}
]
}
}
]
}
}
如果我的JSON文件出现一些语法问题,我深表歉意。我正在尝试访问cachedContents中的item元素。下面是代码
$(document).ready(function () {
$.get("https://data.baltimorecity.gov/api/views/hyq3-8sxr/rows.json", function (data) {
$.each(data.meta.view, function (i, obj) {
if (i == "name")
$("#result").append("<p align='center'>" + obj + "</p>");
if (i == "description")
$("#result").append("<p>" + obj + "</p>");
if (i == "columns")
$.each(obj.i.cachedContents, function (i1, obj) {
$("#result").append("<p>" + "hi" + "</p>");
});
});
});
});
$(文档).ready(函数(){
$.get(”https://data.baltimorecity.gov/api/views/hyq3-8sxr/rows.json,函数(数据){
$.each(data.meta.view,function(i,obj){
如果(i==“名称”)
$(“#结果”)。追加(“”+obj+”
”;
如果(i=“说明”)
$(“#结果”)。追加(“”+obj+””);
如果(i==“列”)
$.each(obj.i.cachedContents,function(i1,obj){
$(“#结果”)。追加(“”+“hi”+“”);
});
});
});
});
任何建议都会非常有用。应该是
$.each(obj[0].cachedContents, function(i1, obj) {
$("#result").append("<p>" + "hi" + "</p>");
});
$.each(obj[0]。缓存内容,函数(i1,obj){
$(“#结果”)。追加(“”+“hi”+“”);
});
对象[0]不是“i”。它应该是
$.each(obj[0].cachedContents, function(i1, obj) {
$("#result").append("<p>" + "hi" + "</p>");
});
$.each(obj[0]。缓存内容,函数(i1,obj){
$(“#结果”)。追加(“”+“hi”+“”);
});
对象[0]不是“i”。您的JSON包含许多字段,在这些字段中迭代并使用
if
可能会出现一些性能问题
您可以直接访问每个成员:
$(文档).ready(函数(){
$.get(”https://data.baltimorecity.gov/api/views/hyq3-8sxr/rows.json“,函数(数据){
$(“#结果”).append(“”+data.meta.view.name+”
”;
$(“#结果”)。追加(“”+data.meta.view.description+””;
data.meta.view.columns.forEach(函数(c){
//这里,“c”是一列
$(“#结果”)。追加(“列”+c.name+””;
});
});
});代码>
您的JSON包含许多字段,在这些字段中迭代并使用if
可能会出现一些性能问题
您可以直接访问每个成员:
$(文档).ready(函数(){
$.get(”https://data.baltimorecity.gov/api/views/hyq3-8sxr/rows.json“,函数(数据){
$(“#结果”).append(“”+data.meta.view.name+”
”;
$(“#结果”)。追加(“”+data.meta.view.description+””;
data.meta.view.columns.forEach(函数(c){
//这里,“c”是一列
$(“#结果”)。追加(“列”+c.name+””;
});
});
});代码>
您从来没有真正访问过item元素……您没有尝试过事件……您从来没有真正访问过item元素……您没有尝试过事件……此代码实际上是否在#result中添加了任何内容?不,我不这么认为。OP可能希望迭代obj来追加某些内容,而不仅仅是obj[0]这段代码实际上在#result中追加了任何内容?不,我不这么认为。OP可能希望迭代obj以附加某些内容,而不仅仅是obj[0],但是,它没有打印其中的缓存内容,对吗?但是,它没有打印其中的缓存内容,对吗?