2017-04-17 19 views
0

Googleナレッジグラフ検索API Linkを使用してGoogleから画像を取得しようとしています。jsonファイルを使用してjsonファイル内の画像のsrcが空であることを確認します。

{ 
    "@context": { 
    "@vocab": "http://schema.org/", 
    "goog": "http://schema.googleapis.com/", 
    "resultScore": "goog:resultScore", 
    "detailedDescription": "goog:detailedDescription", 
    "EntitySearchResult": "goog:EntitySearchResult", 
    "kg": "http://g.co/kg" 
    }, 
    "@type": "ItemList", 
    "itemListElement": [ 
    { 
     "@type": "EntitySearchResult", 
     "result": { 
     "@id": "kg:/m/0dl567", 
     "name": "Taylor Swift", 
     "@type": [ 
      "Thing", 
      "Person" 
     ], 
     "description": "Singer-songwriter", 
     "image": { 
      "contentUrl": "https://t1.gstatic.com/images?q=tbn:ANd9GcQmVDAhjhWnN2OWys2ZMO3PGAhupp5tN2LwF_BJmiHgi19hf8Ku", 
      "url": "https://en.wikipedia.org/wiki/Taylor_Swift", 
      "license": "http://creativecommons.org/licenses/by-sa/2.0" 
     }, 
     "detailedDescription": { 
      "articleBody": "Taylor Alison Swift is an American singer-songwriter and actress. Raised in Wyomissing, Pennsylvania, she moved to Nashville, Tennessee, at the age of 14 to pursue a career in country music. ", 
      "url": "http://en.wikipedia.org/wiki/Taylor_Swift", 
      "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License" 
     }, 
     "url": "http://taylorswift.com/" 
     }, 
     "resultScore": 896.576599 
    } 
    ] 
} 

この目的のためにJavaScriptコードの主要部分は以下の通りである:

<script> 
     var service_url = 'https://kgsearch.googleapis.com/v1/entities:search'; 
     var params = { 
      'query': 'Taylor Swift', 
      'limit': 10, 
      'indent': true, 
      'key' : 'AIzaSyBpCW-EUz2EqI8YIjmQYYXwTzZu8kXGPEw', 
     }; 
     var img; 
     $.getJSON(service_url + '?callback=?', params, function(response) { 
      $.each(response.itemListElement, function(i, element) {   
       img = $('<img>', { 
        src: element['result']['image']['contentUrl'] 
        });  
       if ($(img).attr('src') !== '') { 
        img.appendTo(document.body); 
        }   
       else { 
        $('body').html('<p>image is not available</p>'); 
       } 
      }); 
     }); 
</script> 

あなたはjavascriptのコードから見ることができるように、私はGoogleからの返されたJSONファイルは、以下のこの1に似ていますGoogleナレッジグラフから10枚の画像を取得しようとしています。ただし、Googleナレッジグラフですべての画像を使用できるわけではありません。つまり、一部のURLが空である可能性があります。私は、画像のURLが空でないかどうかをチェックしたい、画像はdocument.bodyに追加されます。そうでないと、のようなエラーメッセージは利用できません。が追加されます。問題は、以下の文が私にとってうまくいかないことです。

私は最初の画像しか得ておらず、残りの部分をチェックするためにループが停止しました。誰かが私のための方向を指すことができますか?私は運がなければ、たくさんのグーグルを探せました。ありがとうございます。

+0

代わりに、なぜあなたはcontentUrl値をチェックしていけないのimg SRCをチェックするの? –

答えて

1
if(element['result']['image']['contentUrl'] != null) { 
     //add image 
     src = element['result']['image']['contentUrl']; 
} else { 
     //show "image is not available" text 
} 
+0

それは私の問題を解決しました。ありがとうございました! – Bigfanx

1

チェックFiddle

JS:

var service_url = 'https://kgsearch.googleapis.com/v1/entities:search'; 
     var params = { 
      'query': 'Taylor Swift', 
      'limit': 10, 
      'indent': true, 
      'key' : 'AIzaSyBpCW-EUz2EqI8YIjmQYYXwTzZu8kXGPEw', 
     }; 
     var img; 
     $.getJSON(service_url + '?callback=?', params, function(response) { 
      $.each(response.itemListElement, function(i, element) { 
      if(typeof element.result.image !== 'undefined') 
      { 
       img = $('<img>', { src: element.result.image.contentUrl });  
       if ($(img).attr('src') !== '') { 
        img.appendTo(document.body); 
        $('body').append('</br>'); 
        }   

       } 
       else { 
        $('body').append('<p>image is not available</p></br>'); 
       } 
      }); 
     }); 
+0

すてきで素早い解決策です。それは私の問題を解決しました。ありがとう、:-) – Bigfanx

+0

Wlcm。答えを受け入れたものとしてマークしてください – RonyLoud

関連する問題