PHP JSON ऑब्जेक्ट में डेटा संभालना


85

JSON में Twitter Search API से रुझान डेटा।

फ़ाइल का उपयोग करके हथियाना:

$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
)
php  json 

जवाबों:


146

क्या आपका मतलब इस तरह की किसी चीज से है?

<?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";
}

क्या $ ट्रेंड ["नाम"] या $ ट्रेंड [] ["नाम"] जैसे लूपिंग के बिना नामों की सूची प्राप्त करने का कोई और तरीका है?
मिन सो

35

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

$array = json_decode($string, true);

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

8

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

$trends = $json_output->trends;
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.