javascriptでJSONのデータを取得しようとしています。
しかし、一部のデータ取得ができず、「Uncaught TypeError: Cannot read property 'url' of undefined」と表示されてしまいます。
自分では何度見ても間違えているように見えないので、どなたかどこが違うか教えて下さい。
なお、「Uncaught TypeError: Cannot read property 'url' of undefined」は
var img = json[i].object.attachments.image.url;
の部分で出ています。
javascript
$(function(){ $.ajax({ type: "GET", url: "https://www.googleapis.com/plus/v1/people/***************/activities/public?key=***************************", dataType: "json", success: function(json){ json = json.items; var num = json.length; var Feed = "<ul>\n"; for(var i=0; i<num; i++){ var title = json[i].title; var link = json[i].url; var icon = json[i].actor.image.url; var name = json[i].actor.displayName; var coment = json[i].title; var img = json[i].object.attachments.image.url; Feed += '<li><span><a href="' + link + '"><img src="' +icon+ '"></a>' + name + '</span>' +'<br>' + coment + '<p><img src="' +img+ '"></p>' + '</li>\n'; } Feed += "</ul>\n"; $("#topics").append(Feed); } }); });
上記URLで取得できたJSON
{ "kind": "plus#activityFeed", "etag": "\"4OZ_Kt6ujOh1jaML_U6RM6APqoE/PrhYcqBsDYTg2BF_P19O8HwfpCw\"", "title": "Google+ List of Activities for Collection PUBLIC", "updated": "2016-04-15T07:16:23.244Z", "items": [ { "kind": "plus#activity", "etag": "\"4OZ_Kt6ujOh1jaML_U6RM6APqoE/Z6VW6M1zVwCl3efyr8MmNgKP5KE\"", "title": "神秘的なシャボン玉♪", "published": "2016-04-15T07:16:23.244Z", "updated": "2016-04-15T07:16:23.244Z", "id": "z12jvreytoe5vnklo220eporttighvqd0", "url": "https://plus.google.com/114624284244149933026/posts/9nW4sCZCJai", "actor": { "id": "114624284244149933026", "displayName": "****", "url": "https://plus.google.com/114624284244149933026", "image": { "url": "https://lh5.googleusercontent.com/-Vy7vaAD81i0/AAAAAAAAAAI/AAAAAAAAABE/mnXdlb-q2VM/photo.jpg?sz=50" }, "verification": { "adHocVerified": "UNKNOWN_VERIFICATION_STATUS" } }, "verb": "post", "object": { "objectType": "note", "actor": { "verification": { "adHocVerified": "UNKNOWN_VERIFICATION_STATUS" } }, "content": "神秘的なシャボン玉♪\ufeff", "url": "https://plus.google.com/114624284244149933026/posts/9nW4sCZCJai", "replies": { "totalItems": 0, "selfLink": "https://www.googleapis.com/plus/v1/activities/z12jvreytoe5vnklo220eporttighvqd0/comments" }, "plusoners": { "totalItems": 0, "selfLink": "https://www.googleapis.com/plus/v1/activities/z12jvreytoe5vnklo220eporttighvqd0/people/plusoners" }, "resharers": { "totalItems": 0, "selfLink": "https://www.googleapis.com/plus/v1/activities/z12jvreytoe5vnklo220eporttighvqd0/people/resharers" }, "attachments": [ { "objectType": "photo", "displayName": "神秘的なシャボン玉♪", "id": "114624284244149933026.6273678319375980162", "content": "shabon.jpg", "url": "https://plus.google.com/photos/114624284244149933026/albums/6273678322613280529/6273678319375980162", "image": { "url": "https://lh3.googleusercontent.com/-IgS76Ke2OGo/VxCVMS15loI/AAAAAAAAAD0/0JgRR4_bQiMM-bl3kIdce4Xu0a6F5H9Qg/w506-h750/shabon.jpg", "type": "image/jpeg" }, "fullImage": { "url": "https://lh3.googleusercontent.com/-IgS76Ke2OGo/VxCVMS15loI/AAAAAAAAAD0/0JgRR4_bQiMM-bl3kIdce4Xu0a6F5H9Qg/w960-h639/shabon.jpg", "type": "image/jpeg", "height": 639, "width": 960 } } ] }, "provider": { "title": "Google+" }, "access": { "kind": "plus#acl", "description": "Public", "items": [ { "type": "public" } ] } },
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/22 03:24
2016/04/22 03:27
2016/04/22 04:48
2016/04/22 05:00
2016/04/22 05:37 編集
2016/04/22 05:40
2016/04/22 05:45
2016/04/22 05:47
2016/04/22 05:49
2016/04/22 05:50
2016/04/22 05:52
2016/04/22 05:54