Jquery 如何调用外部json文件或文件夹中的文件
如何为该代码调用外部json文件或文件夹中的json文件???我的代码的JSFIDLEhttp://jsfiddle.net/binoymat/cve8gyuy/ 我试过了Jquery 如何调用外部json文件或文件夹中的文件,jquery,json,Jquery,Json,如何为该代码调用外部json文件或文件夹中的json文件???我的代码的JSFIDLEhttp://jsfiddle.net/binoymat/cve8gyuy/ 我试过了 $.getJSON( "{% url 'sampletest.json' %}", function(data){ //code inside }); 但是无法提取数据。您需要迭代组和项目。将集合作为第一个参数,data.response.vention.tips.groups.items.text尝试指向字符串。组
$.getJSON( "{% url 'sampletest.json' %}", function(data){
//code inside
});
但是无法提取数据。您需要迭代组和项目。将集合作为第一个参数,data.response.vention.tips.groups.items.text尝试指向字符串。组和项都是数组 详细版本:
$.getJSON(url, function (data) {
// Iterate the groups first.
$.each(data.response.venue.tips.groups, function (index, value) {
// Get the items
var items = this.items; // Here 'this' points to a 'group' in 'groups'
// Iterate through items.
$.each(items, function () {
console.log(this.text); // Here 'this' points to an 'item' in 'items'
});
});
});
或者更简单地说:
$.getJSON(url, function (data) {
$.each(data.response.venue.tips.groups, function (index, value) {
$.each(this.items, function () {
console.log(this.text);
});
});
});
在您指定的JSON中,最后一个是:
$.getJSON(url, function (data) {
// Get the 'items' from the first group.
var items = data.response.venue.tips.groups[0].items;
// Find the last index and the last item.
var lastIndex = items.length - 1;
var lastItem = items[lastIndex];
console.log("User: " + lastItem.user.firstName + " " + lastItem.user.lastName);
console.log("Date: " + lastItem.createdAt);
console.log("Text: " + lastItem.text);
});
这将给你:
用户:Damir p。
日期:1314168377
文字:ajd da vidimo hocu li znati ponoviti
我复制了您的代码并添加了我的测试json文件,您可以看到标题来自json的第一个对象“name”属性: 希望对您有所帮助,我只使用了更通用的ajax jquery fn
$.ajax({
url: "http://mkdizajn.github.io/public/test.json",
success: function(data){
$( 'h1' ).text( data[0].name )
},
});
干杯$。getJSON获取一个已解析的JSON对象。
请参见以下示例:
演示:
$.getJSONhttps://api.myjson.com/bins/4ls7p,函数数据{
//内部代码
var项目=数据;
var checkIds=[];
$.eachitems,functionk,v{
如果$.inarayv.id,则checkId==-1{
$category.append+v.chains+;
检查id.pushv.id;
}
};
$category.on'change',函数{
var dept_number=parseInt$this.val;
var price=$this.find':selected'.data'price';
var-tochange=false;
var合计=0;
$.eachitems,functionk,v{
如果v.id==部门编号{
如果tochange==true{
$category1.append+v.site+;
$category2.append+v.zone+;
$category3.append+v.date+;
}否则{
$category1.html+v.site+;
$category2.html+v.zone+;
$category3.html+v.date+;
改变=正确;
}
总数+=v.访客;
}
};
$‘部门输入’。部门编号;
$‘价格投入’。总计;
}.改变;
$select[id^='category']:notcategory.on'change',函数{
var dept_number=parseInt$this.val;
var price=$this.find':selected'.data'price';
$‘部门输入’。部门编号;
$‘价格输入’。价格;
};
};
部门编号:
价格:
json url是否托管在您请求的同一台主机服务器上?好吧,现在,它位于我的本地驱动器中,稍后将托管在服务器上。。所以,是的。。它在同一台本地服务器上,您是否试图从JSFIDLE访问json文件?否。。我的项目中有一个json文件。。我想调用这个json文件,它在我的项目中,而不是在任何服务器上。。我只是想知道我在汉克斯·基肖尔的小提琴上是怎么做的!!!他们按照我想要的方式工作!!!!Plunker plsi上的某些内容不理解“data.response.venture.tips.groups”是否有人可以修改我的代码并让我知道它是如何完成的?请将您的json文件发送给我。如果对我的eg代码进行更改,将非常有帮助;-在脚本代码中。。。我该怎么办??这是plunker链接。。请编辑这几乎在那里。。。它说项目没有定义我缺少什么??我应该给出var=items;???你能把json粘贴到JSFIDLE的某个地方让我看看吗?