我的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"
}
]
}
获取值:
$code = json_decode($json,true);
foreach($code as $arr) {
foreach($arr as $k=>$v) {
$rt_count = $arr[$k]['rt_count'];
}
}
现在获取实体 - >网址
$img_url = $arr[$k]['entity']['url'];
这是正确的方法吗?
答案 0 :(得分:2)
首先你的json字符串不正确。 结束大括号时出错。我纠正了这个。检查以下内容: -
$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
}
}
]
}
]
}';
$code = json_decode($json,true);
echo "<pre>";print_r($code);echo "<pre>";
如果您只想获得“http://pbs.twimg.com/media/Bev6USHCAAAH5a8.png”,那么以下行将为您提供完美的结果: -
echo $code["results"][0]["entity"][0]["url"];
答案 1 :(得分:1)
你可以这样做:
$code = json_decode($json);
$img_url = $code->results[0]->entity[0]->url;
没有必要循环,除非你拉几个实体而你的json只是不完整。
答案 2 :(得分:1)
试试这个。这样可行。
foreach($code['results'] as $result){
foreach($result['entity'] as $entity){
$img_url = $entity['url'];
echo $img_url . ' ';
}
}