これは説明するのが少し難しいですが、私はそれを説明するために最善を尽くしますので、私はいくつかの助けを得ることができ、うまくいけば、同じJSON内の異なるデータを持つjQuery多次元JSON配列?
基本的に、私はこのようになりますJSONあります
"created_at": "Sun Apr 09 17:56:20 +0000 2017",
"id": 851131679167139842,
"id_str": "851131679167139842",
"text": "hello world with an image goes here as a test! https:\/\/t.co\/jNfdESxPcn",
"truncated": false,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": [],
"media": [{
"id": 851131665623732225,
"id_str": "851131665623732225",
"indices": [47, 70],
"media_url": "http:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
"media_url_https": "https:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
"url": "https:\/\/t.co\/jNfdESxPcn",
"display_url": "pic.twitter.com\/jNfdESxPcn",
"expanded_url": "https:\/\/twitter.com\/DavidHoperz\/status\/851131679167139842\/photo\/1",
"type": "photo",
"sizes": {
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"large": {
"w": 670,
"h": 728,
"resize": "fit"
},
"small": {
"w": 626,
"h": 680,
"resize": "fit"
},
"medium": {
"w": 670,
"h": 728,
"resize": "fit"
}
}
}]
},
"extended_entities": {
"media": [{
"id": 851131665623732225,
"id_str": "851131665623732225",
"indices": [47, 70],
"media_url": "http:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
"media_url_https": "https:\/\/pbs.twimg.com\/media\/C8_TvYqW0AEbBaP.jpg",
"url": "https:\/\/t.co\/jNfdESxPcn",
"display_url": "pic.twitter.com\/jNfdESxPcn",
"expanded_url": "https:\/\/twitter.com\/DavidHoperz\/status\/851131679167139842\/photo\/1",
"type": "photo",
"sizes": {
"thumb": {
"w": 150,
"h": 150,
"resize": "crop"
},
"large": {
"w": 670,
"h": 728,
"resize": "fit"
},
"small": {
"w": 626,
"h": 680,
"resize": "fit"
},
"medium": {
"w": 670,
"h": 728,
"resize": "fit"
}
}
}]
},
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 851119985732800513,
"id_str": "851119985732800513",
"name": "David",
"screen_name": "DavidHoperz",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 0,
"friends_count": 21,
"listed_count": 0,
"created_at": "Sun Apr 09 17:09:52 +0000 2017",
"favourites_count": 0,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 3,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "F5F8FA",
"profile_background_image_url": null,
"profile_background_image_url_https": null,
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"translator_type": "none"
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 0,
"favorite_count": 0,
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"lang": "en"
}, {
"created_at": "Sun Apr 09 17:47:36 +0000 2017",
"id": 851129482832470026,
"id_str": "851129482832470026",
"text": "How to display twitter posts using javascript https:\/\/t.co\/I49vjVbAUJ",
"truncated": false,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": [{
"url": "https:\/\/t.co\/I49vjVbAUJ",
"expanded_url": "https:\/\/www.script-tutorials.com\/how-to-display-twitter-posts-using-javascript\/",
"display_url": "script-tutorials.com\/how-to-display\u2026",
"indices": [46, 69]
}]
},
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 851119985732800513,
"id_str": "851119985732800513",
"name": "David",
"screen_name": "DavidHoperz",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 0,
"friends_count": 21,
"listed_count": 0,
"created_at": "Sun Apr 09 17:09:52 +0000 2017",
"favourites_count": 0,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 3,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "F5F8FA",
"profile_background_image_url": null,
"profile_background_image_url_https": null,
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"translator_type": "none"
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 2,
"favorite_count": 1,
"favorited": false,
"retweeted": false,
"possibly_sensitive": false,
"lang": "en"
}, {
"created_at": "Sun Apr 09 17:33:43 +0000 2017",
"id": 851125986976567297,
"id_str": "851125986976567297",
"text": "Hello world",
"truncated": false,
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
},
"source": "\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e",
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
"id": 851119985732800513,
"id_str": "851119985732800513",
"name": "David",
"screen_name": "DavidHoperz",
"location": "",
"description": "",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 0,
"friends_count": 21,
"listed_count": 0,
"created_at": "Sun Apr 09 17:09:52 +0000 2017",
"favourites_count": 0,
"utc_offset": null,
"time_zone": null,
"geo_enabled": false,
"verified": false,
"statuses_count": 3,
"lang": "en",
"contributors_enabled": false,
"is_translator": false,
"is_translation_enabled": false,
"profile_background_color": "F5F8FA",
"profile_background_image_url": null,
"profile_background_image_url_https": null,
"profile_background_tile": false,
"profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/851182210732494848\/lmgbNLvC_normal.jpg",
"profile_link_color": "1DA1F2",
"profile_sidebar_border_color": "C0DEED",
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"profile_use_background_image": true,
"has_extended_profile": false,
"default_profile": true,
"default_profile_image": false,
"following": false,
"follow_request_sent": false,
"notifications": false,
"translator_type": "none"
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"is_quote_status": false,
"retweet_count": 0,
"favorite_count": 0,
"favorited": false,
"retweeted": false,
"lang": "en"
}]
をそして、私はこのようになりますJSONデータ読み取りにはjQueryを持っている:あなたはJSONを見れば
var poutput = $('.listHolder');
$.ajax({
url: 'page.php',
dataType: 'json',
timeout: 5000,
success: function(data){
$.each(data, function(pi,item){
str = item.id_str;
var products = '<div id="'+item.id_str+'" class="items">'+
'<p class="names">'+item.created_at+'</p>'+
'<p class="names">'+item.text+'</p>'+
'<img src="'+item.entities.media[0].media_url_https+'" width="100%">'+
'</div>';
console.log(products);
poutput.append(products);
});
},
error: function(){
//alert('There was an error loading the data.');
}
});
をデータ(最初のコードは)、一部のデータにはmedia_url_https
が含まれていることがあります。
上記のjQueryコードですべてのJSONデータを取得しようとすると、media_url_https
のデータしか得られず、media_url_https
を持たない他のデータは無視されるため、これが問題になります。
私のコードから'<img src="'+item.entities.media[0].media_url_https+'" width="100%">'+
を削除すると、すべてのデータが取得されますが、画像はありません(画像も必要です)。
イメージなどを含むJSONデータからすべてのデータを取得する方法はありますか?
これは意味があり、誰かがこの問題を解決するのに役立つことを望みます。
ご協力いただければ幸いです。
EDIT:
は、これは私が(場合に使用しようとした方法である)、それ以外()文:
$.each(data, function(pi,item){
str = item.id_str;
var media ="";
var mediaUrl = item.entities.media[0].media_url_https;
if(mediaUrl !=''){
media = '<img src="'+item.entities.media[0].media_url_https+'" width="100%">';
}else{
media = '';
}
//alert(item.entities.media[0].media_url_https);
var products = '<div id="'+item.id_str+'" class="items">'+
'<p class="names">'+item.text+'</p>'+
media+
'</div>';
///reset of my code........
あなたが何を求めているのかは分かりません。そこに常にイメージがあるようにするには、常に 'media_url_https'値が必要です。その値が存在しない場合、どの画像を表示する必要がありますか? –
プロパティをチェックしてそれに応じて反応させるには 'if()'条件が必要です。それを超えると、期待される結果が何であるか明確ではない – charlietfl
@RoryMcCrossan、ごめんなさい。基本的に私はすべてのデータ(画像のあるものと画像のないもの)が必要です。私の現在のjQueryコードでは、私はイメージを持つものとイメージを持たないもののどちらかしか取得できません。 –