2008-11-04 20 views
85

में डेटा हैंडलिंग डेटा।जेएसओएन में ट्विटर खोज एपीआई से एक PHP JSON ऑब्जेक्ट

$jsonurl = "http://search.twitter.com/trends.json"; 
$json = file_get_contents($jsonurl,0,null,null); 
$json_output = json_decode($json); 

मैं इस वस्तु से डेटा के साथ कैसे काम करते हैं:

फ़ाइल का उपयोग किए जा रहे हैं। एक सरणी के रूप में? केवल [नाम] मानों से डेटा निकालने की आवश्यकता है।

JSON ऑब्जेक्ट शामिल हैं:

stdClass Object 
(
    [trends] => Array 
     (
      [0] => stdClass Object 
       (
        [name] => Vote 
        [url] => http://search.twitter.com/search?q=Vote 
       ) 

      [1] => stdClass Object 
       (
        [name] => Halloween 
        [url] => http://search.twitter.com/search?q=Halloween 
       ) 

      [2] => stdClass Object 
       (
        [name] => Starbucks 
        [url] => http://search.twitter.com/search?q=Starbucks 
       ) 

      [3] => stdClass Object 
       (
        [name] => #flylady 
        [url] => http://search.twitter.com/search?q=%23flylady 
       ) 

      [4] => stdClass Object 
       (
        [name] => #votereport 
        [url] => http://search.twitter.com/search?q=%23votereport 
       ) 

      [5] => stdClass Object 
       (
        [name] => Election Day 
        [url] => http://search.twitter.com/search?q=%22Election+Day%22 
       ) 

      [6] => stdClass Object 
       (
        [name] => #PubCon 
        [url] => http://search.twitter.com/search?q=%23PubCon 
       ) 

      [7] => stdClass Object 
       (
        [name] => #defrag08 
        [url] => http://search.twitter.com/search?q=%23defrag08 
       ) 

      [8] => stdClass Object 
       (
        [name] => Melbourne Cup 
        [url] => http://search.twitter.com/search?q=%22Melbourne+Cup%22 
       ) 

      [9] => stdClass Object 
       (
        [name] => Cheney 
        [url] => http://search.twitter.com/search?q=Cheney 
       ) 

     ) 

    [as_of] => Mon, 03 Nov 2008 21:49:36 +0000 
) 

उत्तर

147

आप कुछ इस तरह मतलब है?

<?php 

$jsonurl = "http://search.twitter.com/trends.json"; 
$json = file_get_contents($jsonurl,0,null,null); 
$json_output = json_decode($json); 

foreach ($json_output->trends as $trend) 
{ 
    echo "{$trend->name}\n"; 
} 
+0

यह बहुत अच्छा है। धन्यवाद। –

+18

आसान प्रतिनिधि बिंदु, हुह? =) – Seiti

+3

बिल्कुल मुझे जो चाहिए वह भी! धन्यवाद! –

8

बस इसका उपयोग करें जैसे कि यह एक वस्तु है जिसे आपने परिभाषित किया था। अर्थात

$trends = $json_output->trends; 
35

आप json_decode($string, true) का उपयोग करते हैं, तो आप कोई वस्तुओं मिलता है, लेकिन एक साहचर्य या संख्या अनुक्रमित सरणी के रूप में सब कुछ होगा। रास्ता संभालना आसान है, क्योंकि PHP द्वारा प्रदान की गई stdObject सार्वजनिक गुणों के साथ एक गूंगा कंटेनर नहीं है, जिसे आपकी अपनी कार्यक्षमता के साथ विस्तारित नहीं किया जा सकता है।

$array = json_decode($string, true); 

echo $array['trends'][0]['name']; 
-2

साफ तरीका होगा:

$jsonurl = 'http://search.twitter.com/trends.json'; 
$json = file_get_contents($jsonurl, 0, null, null); 
$json_output = json_decode($json, true); 
$trends = $json_output['trends']; 

foreach ($trends as $trend) { 
    your_func($trend['name']); 
} 
संबंधित मुद्दे