سؤال

I have the following JSON result , where am trying to extract the value of description and title and trying to bind it to html placeholder, But i am getting a error while parsing the JSON saying Uncaught TypeError: Cannot read property 'length' of undefined

Jquery Code :

 $.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 Result

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"
    }
]
})
هل كانت مفيدة؟

المحلول

Your description and title are nested inside content, so you need to do:

$.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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top