2014-01-25 411 views
1

我的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']; 
    } 
} 

現在拿到實體 - > URL

$img_url = $arr[$k]['entity']['url']; 

是這個正確的方式?

+1

JSON字符串是無效的首先。在這種情況下,json_decode()將返回NULL。 –

回答

1

你可以只是這樣做:

$code = json_decode($json); 
$img_url = $code->results[0]->entity[0]->url; 

有通過無需循環,除非你是拉幾個實體和你的JSON就是不完整的。

+0

我得到了幾個結果,並希望爲它做所有 –

1

試試這個。這會工作。

foreach($code['results'] as $result){ 
    foreach($result['entity'] as $entity){ 
    $img_url = $entity['url']; 
    echo $img_url . ' '; 
    } 
} 
+0

@Jinging:你能看到我更新的闕。我需要爲每個結果分別獲取每個img –

+0

是的,這會爲每個結果獲取img。它們被一個空格分隔('echo $ img_url。'';')。 –

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"]; 
+0

謝謝,但在這種情況下,它始終打印相同的圖像網址。我把'我= 0'和增量獲得每個網址,但不工作 –

+0

每個網址意味着什麼? – ripa

+0

請參閱我的更新。我有一套結果。對於每個結果,我想要推文,標題,情緒,實體 - >網址。 –