2014-03-28 42 views
0

我有以下的JSON的結果,在這裏我試圖提取descriptiontitle的價值,並試圖將其綁定到HTML佔位符,但我得到一個錯誤,而解析JSON說遺漏的類型錯誤:無法讀取未定義錯誤與解析嵌套的JSON結果

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" 
    } 
] 
}) 
+0

cs.description和cs.title嵌在兩個份額和內容。 –

回答

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

+0

謝謝:)這工作正常,我可以請知道爲什麼你解析時跳過「共享」節點? – mahesh

+1

這是因爲你的'companyStatusUpdate'只有一個'share'節點,所以你的'share'節點在每個循環內都等於'cs'。 – Felix