通过字段名连接Javascript中的JSON
Connect JSON in Javascript via field names
我正在尝试将JSON数据连接到我的Javascript中,JSON看起来像这样:
{
"status":"ok",
"count":5,
"pages":1,
"category":{
"id":85,
"slug":"front-page-active",
"title":"Front Page Active",
"description":"",
"parent":0,
"post_count":5
},
"posts":[{
"id":1535,
"type":"post",
"slug":"url",
"url":"url/",
"status":"publish",
"title":"title",
"title_plain":"title",
"content":"content… …
我的Javscript看起来是这样的:
'<p class="listTitle">' + item.title + '</p>'
我知道我正确地连接了该文件,因为我已经将其用于其他JSON文件。我认为这个文件的设置不同,我只是不知道我缺少了什么。我必须修改吗项目标题呼叫
这是完整的javascript文件:
$(document).ready(function(){
var outputNews = $('#newsPageUl');
var spinner = $('.pageLoaderSpinner');
$.ajax({
url: 'url',
dataType: 'json',
timeout: 20000,
cache: false,
success: function(data, status){
$.each(data, function(i,item){
var newsEvent =
//Generated HTML
'<li><div class="newsFeedTopBg"></div><div class="newsFeedMidBg">'
+ '<p class="listTitle">' + item.title + '</p>'
// + '<p class="date">' + item.posts[0].date + '</p>'
// + '<p class="pContent">' + item.posts[0].content + '</p>'
+ '</div><div class="newsFeedBottomBg"></div></li>'
//End Generated HTML
;
spinner.empty();
outputNews.append(newsEvent);
});
},
error: function(){
spinner.empty();
navigator.notification.confirm(
"Could not connect.",
connectionErrorNews,
'Connection Error',
"Go Back, Try Again"
);
}
});
});
在您的JavaScript文件中,您有一行:
$.each(data, function(i,item){
继续并将其更改为:
$.each(data.posts, function(i,item){
这将把item
变量设置为每个帖子的帖子对象。帖子有标题,所以现在一切都应该正常。
var data = {
"status": "ok",
"count": 5,
"pages": 1,
"category": {
"id": 85,
"slug": "front-page-active",
"title": "Front Page Active",
"description": "",
"parent": 0,
"post_count": 5
},
"posts": [{
"id": 1535,
"type": "post",
"slug": "haverhill-public-schools-holiday-events-calendar",
"url": "http:'/'/www.haverhill-ps.org'/haverhill-public-schools-holiday-events-calendar'/",
"status": "publish",
"title": "Haverhill Public Schools holiday events calendar"
}, {
"id": 1536,
"type": "post",
"slug": "haverhill-public-schools-holiday-events-calendar",
"url": "http:'/'/www.haverhill-ps.org'/haverhill-public-schools-holiday-events-calendar'/",
"status": "publish",
"title": "Haverhill Public Schools holiday events calendar"
}]};
for(key in data) {
if(key == "category")
alert(data["category"].title);
if(key == "posts")
data["posts"].forEach(function(e) {
alert(e.title);
});
}
- http://jsfiddle.net/3T7tC/2/
相关文章:
- javascript中的数组出错
- JavaScript中的函数和对象之间没有区别吗?
- Javascript中的空白是什么
- 为什么要使用0>javascript中的0
- 如何制作简单的php'在Javascript中的foreach等价物
- javascript中的布尔条件
- 我可以'我似乎不知道如何修复javascript中的两个lint.有人能帮我理解吗
- 验证Javascript中的Textarea
- 可以't使用Appcelerator将JSON文件解析为JavaScript中的TableView
- 是“;x==a||b”;以及“;x==a||x==b”;JavaScript中的等效语句
- 仅在IE中,javascript中的时区名称不正确
- JavaScript 中的嵌套函数和 “this” 关键字
- javascript中的命名空间,IDE中支持代码完成/内容辅助's
- JavaScript 中的高阶函数
- Javascript中的备选注释方法
- 如何使用JavaScript中的用户输入创建序列/序列
- 无法理解JavaScript中的静态方法
- 如何附加'#'到Javascript中的值
- javascript 中的全局函数
- 用javascript中的param执行动态函数