1

我无法显示我的一些 json 项目,尤其是所有custom_fields项目。请在下面找到我的 json 和 jquery 代码。

========== JSON ===============

 {
  "status": "ok",
  "count": 1,
  "count_total": 1,
  "pages": 1,
  "posts": [
   {
  {
  "id": 89,
  "type": "events",
  "slug": "%d0%bf%d0%be%d0%bb%d0%be%d0%b2%d0%be%d0%b4%d1%8c%d0%b5",
  "url": "https:\/\/domain.com\/events\/%d0%bf%d0%be%d0%bb%d0%be%d0%b2%d0%be%d0%b4%d1%8c%d0%b5\/",
  "status": "publish",
  "title": "\u041f\u043e\u043b\u043e\u0432\u043e\u0434\u044c\u0435",
  "title_plain": "\u041f\u043e\u043b\u043e\u0432\u043e\u0434\u044c\u0435",
  "content": "<p>\u0424\u0435\u0441\u0442\u0438\u0432\u0430\u043b\u044c \u043c\u0443\u0437\u044b\u043a\u0438 \u0438 \u043c\u043e\u0434\u044b<\/p>\n",
  "excerpt": "\u0424\u0435\u0441\u0442\u0438\u0432\u0430\u043b\u044c \u043c\u0443\u0437\u044b\u043a\u0438 \u0438 \u043c\u043e\u0434\u044b",
  "date": "2013-06-25 09:26:49",
  "modified": "2013-06-26 10:18:13",
  "categories": [],
  "tags": [],
  "author": {
    "id": 1,
    "slug": "vzakharov",
    "name": "vzakharov",
    "first_name": "",
    "last_name": "",
    "nickname": "vzakharov",
    "url": "",
    "description": ""
  },
  "comments": [],
  "attachments": [],
  "comment_count": 0,
  "comment_status": "closed",
  "custom_fields": {
    "wpcf-event-date": [
      "1373130000"
    ],
    "wpcf-venue": [
      "\u041f\u043b\u043e\u0449\u0430\u0434\u044c \u043d\u0430\u0440\u043e\u0434\u043d\u044b\u0445 \u0433\u0443\u043b\u044f\u043d\u0438\u0439"
    ]
  }
}
  ]
 }

============= JQUERY ==================

 $(function () {
  $.ajax({
    url: 'link_to_json',
    async: false,
    callback: 'callback',
    crossDomain: true,
    contentType: 'application/json; charset=utf-8',
    type: 'POST',
    dataType: 'jsonp',
    success: function (data, status) {
        if (data !== undefined && data.posts !== undefined) {

            $.each(data.posts, function (i, item) {
                $('#dayevent').append('<span>' + item.title + item.custom_fields[0].wpcf-venue + item.custom_fields[0].wpcf-event-date +'</span>');
            });
        }

    },
    error: function () {
        $('#news').append('No connection.');
    }
});
});

并将wpcf-event-date的时间戳显示为正常日期。提前谢谢你的帮助。

4

1 回答 1

1

This is not working because your custom_fields property is an Object, not an array. This means that your index custom_fields[0] isn't going to work as you intend. To get the proper value out of your custom_fields object you have to index it with your key, which in this case is wpcf-venue

item.custom_fields['wpcf-event-date']; // "1373130000"

See my jsfiddle if you're still confused.

fiddle

EDIT: with regards to showing wpcf-event-date as a normal date, you can use date libraries such as momentjs which I would recommend, or do something similar to this:

Date d = new Date(1373130000 * 1000);

Where 1373130000 is your unix timestamp.

于 2013-06-26T19:44:22.917 回答