解析嵌套的json结果时出错

时间:2014-03-28 08:46:54

标签: jquery json jsonp

我有以下JSON结果,我尝试提取 description title 的值并尝试将其绑定到html占位符,但我在解析JSON时遇到错误未捕获的TypeError:无法读取未定义的属性“长度”

Jquery代码:

 $.each(data.values, function (index, value) {
     $.each(value.updateContent, function (i, uc) {
            $.each(uc.companyStatusUpdate, function (j, cs) {
                $("#link").append('<li ><a href="#"> ' + cs.description + '</li>');
                $("#link").append('<li ><a href="#"> ' + cs.title + '</li>');
                $("#link").listview('refresh');
                console.log(uc);
             });
     });
});

JSON结果

jQuery17104({
  "_count": 10,
  "_start": 0,
  "_total": 25,
  "values": [
    {
      "isCommentable": true,
      "isLikable": true,
      "timestamp": 1395930772462,
      "updateContent": {
        "company": {
          "id": 1284,
          "name": "Credit Suisse"
        },
        "companyStatusUpdate": {"share": {
          "content": {
            "description": "Watch Mr. English\u2019s Conversation with Credit Suisse at the 17th Annual Asian Investment Conference. The \"AIC Conversations\" series highlights one-on-one interviews with keynote speakers participating in the 2014 conference.",
            "eyebrowUrl": "http://www.linkedin.com/share?viewLink=&sid=s5854958022744379392&url=http%3A%2F%2Flnkd%2Ein%2FdQMxR6R&urlhash=cLje&uid=5854958022647902208",
            "shortenedUrl": "http://lnkd.in/dQMxR6R",
            "submittedImageUrl": "http://image-store.slidesharecdn.com/6fbb4010-b5bc-11e3-a4b9-12313d239d6c-large.jpeg",
            "submittedUrl": "https://www.credit-suisse.com/sites/conferences/aic/en/media-hub/videos/bill-english.html",
            "thumbnailUrl": "https://media.licdn.com/media-proxy/ext?w=80&h=100&hash=oTIIzMKu3QdRr34Tle51XeCMBN0%3D&url=http%3A%2F%2Fimage-store.slidesharecdn.com%2F6fbb4010-b5bc-11e3-a4b9-12313d239d6c-large.jpeg",
            "title": "Bill English, MP, Deputy PM and Minister of Finance, New Zealand"
          },
          "id": "s5854958022744379392",
          "source": {"serviceProvider": {"name": "LINKEDIN"}},
          "timestamp": 1395930772462,
          "visibility": {"code": "anyone"}
        }}
      },
      "updateKey": "UPDATE-c1284-5854958022647902208",
      "updateType": "CMPY"
    },
    {
      "isCommentable": true,
      "isLikable": true,
      "timestamp": 1395854948871,
      "updateContent": {
        "company": {
          "id": 1284,
          "name": "Credit Suisse"
        },
        "companyStatusUpdate": {"share": {
          "content": {
            "description": "Dr. Rakesh Mohan, Executive Director of the International Monetary Fund, discusses India\u2019s economy, the lessons of the financial crisis, and the future of central banking.",
            "eyebrowUrl": "http://www.linkedin.com/share?viewLink=&sid=s5854639995586912256&url=http%3A%2F%2Flnkd%2Ein%2FdMTRVWX&urlhash=_tMo&uid=5854639995461070848",
            "shortenedUrl": "http://lnkd.in/dMTRVWX",
            "submittedImageUrl": "http://image-store.slidesharecdn.com/ce4a0f96-b50b-11e3-9e7d-12313d026081-large.jpeg",
            "submittedUrl": "https://www.credit-suisse.com/sites/conferences/aic/en/media-hub/videos/rakesh-mohan.html",
            "thumbnailUrl": "https://media.licdn.com/media-proxy/ext?w=80&h=100&hash=%2BFR4ugOqYRSWvC4iQRd1372beTs%3D&url=http%3A%2F%2Fimage-store.slidesharecdn.com%2Fce4a0f96-b50b-11e3-9e7d-12313d026081-large.jpeg",
            "title": "Rakesh Mohan, IMF, on India, the Financial Crisis, and Central Banking"
          },
          "id": "s5854639995586912256",
          "source": {"serviceProvider": {"name": "LINKEDIN"}},
          "timestamp": 1395854948871,
          "visibility": {"code": "anyone"}
        }}
      },
      "updateKey": "UPDATE-c1284-5854639995461070848",
      "updateType": "CMPY"
    }
]
})

1 个答案:

答案 0 :(得分:1)

您的descriptiontitle嵌套在content内,因此您需要执行以下操作:

$.each(data.values, function (index, value) {
    $.each(value.updateContent.companyStatusUpdate, function (j, cs) {
        $("#link").append('<li ><a href="#"> ' + cs.content.description + '</li>');
        $("#link").append('<li ><a href="#"> ' + cs.content.title + '</li>');
        $("#link").listview('refresh');
        console.log(uc);
    });
}); 

<强> Fiddle Demo