通过字段名连接Javascript中的JSON

Connect JSON in Javascript via field names

本文关键字:Javascript 中的 JSON 连接 字段      更新时间:2024-03-22

我正在尝试将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);
      });
}

  1. http://jsfiddle.net/3T7tC/2/