我已经完成了类似的线程,但没有得到解决方案。
这是我的json结果:
{
"status":"ok",
"message":"Success v1.",
"remainingquota":"200",
"results":[
{
"timestamp":"2014-01-24 23:26:55",
"id":"426709995816624128",
"category":"sports",
"username":"KiranKS",
"displayname":"KiranKS",
"profileimage":"http://pbs.twimg.com/profile_images/415170263957778432/eS9f8sz0_normal.jpeg",
"tweet_text":"#Respect. 5 year old Hari Charan Rao participated in Mumbai marathon, on a prosthetic leg -> http://t.co/8YknB78WOU",
"title":"#Respect. 5 year old Hari Charan Rao participated in Mumbai marathon, on a prosthetic leg -> http://t.co/8YknB78WOU",
"city":"mumbai",
"rt_count":"43",
"sentiment":"0",
"powertweet":"0",
"entity":[
{
"url":"http://pbs.twimg.com/media/Bev6USHCAAAH5a8.png",
"type":"img",
"info":{
"type":"img",
"h":260,
"w":339
}
}
],
"updated":"18:12:10"
},
{
"timestamp":"2014-01-24 15:16:48",
"id":"426635550083534849",
"category":"sports",
"username":"LFCIndia",
"displayname":"LFCIndia",
"profileimage":"http://pbs.twimg.com/profile_images/3005882644/6a76508e39ce2105e88bea69b24e576c_normal.jpeg",
"tweet_text":"Happy Birthday @luis16suarez! #JustCantGetEnough #LFCIndia http://t.co/Xv4f3qlVUL",
"title":"Happy Birthday @luis16suarez! #JustCantGetEnough #LFCIndia http://t.co/Xv4f3qlVUL",
"city":"new delhi",
"rt_count":"13",
"sentiment":"0",
"powertweet":"0",
"entity":[
{
"url":"http://pbs.twimg.com/media/Beu2kqYCQAAr6iu.jpg",
"type":"img",
"info":{
"type":"img",
"h":340,
"w":340
}
}
],
"updated":"23:07:59"
},
{
"timestamp":"2014-01-25 03:49:09",
"id":"426879169273028608",
"category":"sports",
"username":"AltCricket",
"displayname":"Alternative Cricket",
"profileimage":"http://pbs.twimg.com/profile_images/378800000739216153/e99558c729acccbd6d0d4950b7257044_normal.jpeg",
"tweet_text":"Ishant Sharma has been dropped for India. I guess he didn't see it coming: http://t.co/mXvtHGLWxl",
"title":"Ishant Sharma has been dropped for India. I guess he didn't see it coming: http://t.co/mXvtHGLWxl",
"city":"india",
"rt_count":"10",
"sentiment":"0",
"powertweet":"1",
"entity":[
{
"url":"http://pbs.twimg.com/media/BeyTL8hCIAAqX4P.jpg",
"type":"img"
}
],
"updated":"06:59:56"
},
{
"timestamp":"2014-01-24 19:24:47",
"id":"426628493196029952",
"category":"sports",
"username":"Sou_Reddevil",
"displayname":"Sou_Reddevil",
"profileimage":"http://pbs.twimg.com/profile_images/423135765434880000/vtOHs2PD_normal.jpeg",
"tweet_text":"Still a better negotiator than Ed Woodward #MUFC @aditya_reds http://t.co/uVNQhwCHxq",
"title":"Still a better negotiator than Ed Woodward #MUFC @aditya_reds http://t.co/uVNQhwCHxq",
"city":"hyderabad",
"rt_count":"9",
"sentiment":"40",
"powertweet":"0",
"entity":[
{
"url":"http://pbs.twimg.com/media/Beuv49RCIAEtbnF.jpg",
"type":"img",
"info":{
"type":"img",
"h":511,
"w":339
}
}
],
"updated":"23:36:02"
},
{
"timestamp":"2014-01-24 14:03:46",
"id":"426695551275393025",
"category":"sports",
"username":"DDNewsLive",
"displayname":"DDNewsLive",
"profileimage":"http://pbs.twimg.com/profile_images/3295146457/bcc4f0e184c107b1c8f4db9acb8d542b_normal.jpeg",
"tweet_text":"All in good spirit: #RafaelNadal, #RogerFederer hug after Nadal won men's singles semi-final match at #AustralianOpen http://t.co/ThFiahlRl6",
"title":"All in good spirit: #RafaelNadal, #RogerFederer hug after Nadal won men's singles semi-final match at #AustralianOpen http://t.co/ThFiahlRl6",
"city":"new delhi",
"rt_count":"8",
"sentiment":"45",
"powertweet":"0",
"entity":[
{
"url":"http://pbs.twimg.com/media/BevtBXhCQAIlxUj.jpg",
"type":"img",
"info":{
"type":"img",
"h":273,
"w":340
}
}
],
"updated":"19:09:34"
},
{
"timestamp":"2014-01-25 00:05:03",
"id":"426785995208802305",
"category":"sports",
"username":"ScholesLegacy",
"displayname":"ScholesLegacy",
"profileimage":"http://pbs.twimg.com/profile_images/424796863066144768/gF95iWs-_normal.jpeg",
"tweet_text":"Agent Rooney brilliant on the job. http://t.co/7gHxFDeanv",
"title":"Agent Rooney brilliant on the job. http://t.co/7gHxFDeanv",
"city":"mumbai",
"rt_count":"7",
"sentiment":"80",
"powertweet":"0",
"entity":[
{
"url":"http://pbs.twimg.com/media/Bew_eqJCAAAmJ5l.jpg",
"type":"img",
"info":{
"type":"img",
"h":511,
"w":339
}
}
],
"updated":"13:10:10"
},
{
"timestamp":"2014-01-24 18:32:40",
"id":"426712202591354880",
"category":"sports",
"username":"DelhiDaredevils",
"displayname":"DelhiDaredevils",
"profileimage":"http://pbs.twimg.com/profile_images/412911860107595776/MYQ8boPd_normal.jpeg",
"tweet_text":"#DD congratulates Women's Cricket Team Captain Mithali Raj! Her ton helped India win 3 match series against Sri Lanka http://t.co/kfBlMmfGLy",
"title":"#DD congratulates Women's Cricket Team Captain Mithali Raj! Her ton helped India win 3 match series against Sri Lanka http://t.co/kfBlMmfGLy",
"city":"india",
"rt_count":"6",
"sentiment":"80",
"powertweet":"0",
"entity":[
{
"url":"http://pbs.twimg.com/media/BewB_L-CQAIW2MS.jpg",
"type":"img",
"info":{
"type":"img",
"h":340,
"w":340
}
}
],
"updated":"17:54:15"
},
{
"timestamp":"2014-01-24 17:41:39",
"id":"426767605664186368",
"category":"sports",
"username":"ShivamYagnik",
"displayname":"ShivamYagnik",
"profileimage":"http://pbs.twimg.com/profile_images/378800000762607882/34903620999e5a5b26a502ffd0deb608_normal.jpeg",
"tweet_text":""@Unitedology: Juan Mata's friend and hairdresser confirms he has joined United on Instagram... http://t.co/OTrZTnN4b5"@utkarshvmehta",
"title":""@Unitedology: Juan Mata's friend and hairdresser confirms he has joined United on Instagram... http://t.co/OTrZTnN4b5"@utkarshvmehta",
"city":"mumbai",
"rt_count":"6",
"sentiment":"0",
"powertweet":"0",
"entity":[
{
"url":"http://pbs.twimg.com/media/Bews-uYIgAAiwPC.jpg",
"type":"img",
"info":{
"type":"img",
"h":605,
"w":340
}
}
],
"updated":"14:23:15"
},
{
"timestamp":"2014-01-24 15:19:11",
"id":"426706363931037696",
"category":"sports",
"username":"karthik_CFC",
"displayname":"karthik_CFC",
"profileimage":"http://pbs.twimg.com/profile_images/422725285780275200/2YEBc6C7_normal.jpeg",
"tweet_text":"Someone from Fb posted it.,just wanted to share with loyal Chelsea fans,not Mata fan boys.,#CFC http://t.co/dB6Mz3s2vs",
"title":"Someone from Fb posted it.,just wanted to share with loyal Chelsea fans,not Mata fan boys.,#CFC http://t.co/dB6Mz3s2vs",
"city":"bangalore",
"rt_count":"4",
"sentiment":"47",
"powertweet":"0",
"entity":[
{
"url":"http://pbs.twimg.com/media/Bev3DunIEAA0s_p.jpg",
"type":"img",
"info":{
"type":"img",
"h":604,
"w":340
}
}
],
"updated":"18:26:36"
},
{
"timestamp":"2014-01-24 09:12:08",
"id":"426639734446645250",
"category":"sports",
"username":"Gotham3",
"displayname":"Gotham3",
"profileimage":"http://pbs.twimg.com/profile_images/1839631891/320183_10150876927320227_848920226_21226871_267543349_n_normal.jpg",
"tweet_text":"Indian cricketer Sreesanth getting weighed with bananas on scale!! http://t.co/Hjgwe2jO5b",
"title":"Indian cricketer Sreesanth getting weighed with bananas on scale!! http://t.co/Hjgwe2jO5b",
"city":"new delhi",
"rt_count":"3",
"sentiment":"0",
"powertweet":"0",
"entity":[
{
"url":"http://pbs.twimg.com/media/Beu5cSgCMAEhMMz.jpg",
"type":"img",
"info":{
"type":"img",
"h":228,
"w":339
}
}
],
"updated":"22:51:22"
}
]
}
我能够获得每个结果的情绪,推文,rt_count。但在获得entity - > url
时面临问题。
我想为每个结果单独打印以下值。
rtcount,情感,推文,标题[这些正确有效]和实体 - >网址
代码:
$code = json_decode($json,true);
foreach($code as $arr)
{
$i = 0;
foreach($arr as $k=>$v)
{
//$counter to restrict up to 5 articles
$tweet1 = $arr[$k]['tweet_text'];
$rt_count = $arr[$k]['rt_count'];
$sentiment = $arr[$k]['sentiment'];
$title = $arr[$k]['title'];
echo $title;
echo $arr[$k]['entity'][0]->url;
//how to get url?????
//echo $code->results[$i]->entity[$i]->url; does not work
}}
PHP代码:
<?php
$feedUrl = "http://api.frrole.com/v1/curated-content?location=India&contenttype=image&orderby=popularity&minrt=50&resultcount=2&apikey=7fRJ1xtXUOTbATXoqv8U52e358e4caae5"; //Get popular urls
$flag = 1; //1 for popular articles
$category = 'sports';
make_frrole_call($feedUrl,$flag);
function make_frrole_call($feedUrl,$flag)
{
$json = file_get_contents($feedUrl);
$code = json_decode($json,true);
getTopTweets($code,$flag);
}
function getTopTweets($code,$flag)
{
foreach($code as $arr)
{
$i = 0;
foreach($arr as $k=>$v)
{
//$counter to restrict up to 5 articles
$tweet1 = $arr[$k]['tweet_text'];
$rt_count = $arr[$k]['rt_count'];
$sentiment = $arr[$k]['sentiment'];
echo "Image : $arr[$k]['entity'][0]['url']";
echo " <br/>Tweet : $tweet1 <br>";
echo "Retweet count is : ",$arr[$k]['rt_count']."<br/>";
}
}
}
?>
答案 0 :(得分:4)
由于entity
是一个数组而不是一个对象,所以你应该按原样处理它,例如:
$arr[$k]['entity'][0]['url'];
如你的例子所示:
$code = json_decode($json,true);
foreach($code as $arr)
{
$i = 0;
foreach($arr as $k=>$v)
{
//$counter to restrict up to 5 articles
$tweet1 = $arr[$k]['tweet_text'];
$rt_count = $arr[$k]['rt_count'];
$sentiment = $arr[$k]['sentiment'];
$title = $arr[$k]['title'];
echo $arr[$k]['entity'][0]['url'];
}
}
显然,在这种情况下考虑使用foreach
语句来处理整个数组。
请参阅我用于调试您的问题的代码:
$code = json_decode('{
"status":"ok",
"message":"Success v1.",
"remainingquota":"200",
"results":[
{
"timestamp":"2014-01-24 23:26:55",
"id":"426709995816624128",
"category":"sports",
"username":"KiranKS",
"displayname":"KiranKS",
"profileimage":"http://pbs.twimg.com/profile_images/415170263957778432/eS9f8sz0_normal.jpeg",
"tweet_text":"#Respect. 5 year old Hari Charan Rao participated in Mumbai marathon, on a prosthetic leg -> http://t.co/8YknB78WOU",
"title":"#Respect. 5 year old Hari Charan Rao participated in Mumbai marathon, on a prosthetic leg -> http://t.co/8YknB78WOU",
"city":"mumbai",
"rt_count":"43",
"sentiment":"0",
"powertweet":"0",
"entity":[
{
"url":"http://pbs.twimg.com/media/Bev6USHCAAAH5a8.png",
"type":"img",
"info":{
"type":"img",
"h":260,
"w":339
}
}
],
"updated":"18:12:10"
}
]
}',true);
foreach($code as $arr)
{
$i = 0;
foreach($arr as $k=>$v)
{
//$counter to restrict up to 5 articles
$tweet1 = $arr[$k]['tweet_text'];
$rt_count = $arr[$k]['rt_count'];
$sentiment = $arr[$k]['sentiment'];
$title = $arr[$k]['title'];
echo $arr[$k]['entity'][0]['url'];
}
}
答案 1 :(得分:3)
实体是一个包含1个项目的数组,因此您需要包含索引
$arr[$k]['entity'][0]->url;
注意[0]
是entity