2009-08-13 13 views
6

मेरे पास निम्न जेसन स्ट्रिंग है और मैं इसे केवल ईमेल पता पुनर्प्राप्त करना चाहता हूं। मैं इसे php में कैसे करूं?PHP में जेसन डीकोड

{"communications":{"communication":[{"@array":"true","@id":"23101384","@uri":"xyz/v1/Communications/1111","household":{"@id":"111111","@uri":"xyz/v1/Households/5465465"},"person":{"@id":"","@uri":""},"communicationType":{"@id":"1","@uri":"xyz/v1/Communications/CommunicationTypes/1","name":"Home Phone"},"communicationGeneralType":"Telephone","communicationValue":"1111","searchCommunicationValue":"2693240758","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/111111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/11111"},"person":{"@id":"2222222","@uri":"xyz/v1/People/22222222"},"communicationType":{"@id":"2","@uri":"xyz/v1/Communications/CommunicationTypes/2","name":"Work Phone"},"communicationGeneralType":"Telephone","communicationValue":"11111","searchCommunicationValue":"789787987","listed":"false","communicationComment":null,"createdDate":"2009-08-09T15:49:27","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/11111","household":{"@id":"1111","@uri":"xyz/v1/Households/1111"},"person":{"@id":"244404","@uri":"xyz/v1/People/1111"},"communicationType":{"@id":"3","@uri":"xyz/v1/Communications/CommunicationTypes/3","name":"Mobile"},"communicationGeneralType":"Telephone","communicationValue":"22222","searchCommunicationValue":"5475454","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"15454","@uri":"xyz/v1/Communications/111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/1111"},"person":{"@id":"244444474","@uri":"xyz/v1/People/111111"},"communicationType":{"@id":"4","@uri":"xyz/v1/Communications/CommunicationTypes/4","name":"Email"},"communicationGeneralType":"Email","communicationValue":"[email protected]","searchCommunicationValue":"[email protected]","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:39:06"}]}} 
+1

मैं इस सवाल सिर्फ था इच्छा: मैं एक JSON इनकोडिंग स्ट्रिंग है और इसे से ईमेल पता पुनः प्राप्त करना चाहते हैं, लेकिन समझ में नहीं आता कि कैसे json_decode का उपयोग करें और परिणामों से मान प्राप्त करने के लिए। क्या कोई मुझे PHP में ऐसा करने में मदद कर सकता है। मैं सवालों के लिए और अधिक मुश्किल होने के लिए उपयोग किया जाता है और इसने मुझे फेंक दिया। :) – TheJacobTaylor

+0

एक्स-रेफ: [print_r() के आउटपुट में एक चर देखने के लिए सक्षम है, लेकिन यह सुनिश्चित नहीं है कि कोड में इसका उपयोग कैसे करें] (http://stackoverflow.com/q/6322084/367456) – hakre

उत्तर

8

कैसे inerte इसे उपयोग करने में किया जाएगा था पर एक और मोड़ इसे पसंद:

$json_object = '{"communications": 
         {"communication": 
         [{"@array":"true","@id":"23101384","@uri":"xyz/v1/Communications/1111","household": 
          {"@id":"111111","@uri":"xyz/v1/Households/5465465"}, 
          "person": {"@id":"","@uri":""}, 
          "communicationType":{"@id":"1","@uri":"xyz/v1/Communications/CommunicationTypes/1","name":"Home Phone"}, 
          "communicationGeneralType":"Telephone","communicationValue":"1111","searchCommunicationValue":"2693240758", 
           "listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"}, 
          {"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/111111111","household": 
           {"@id":"14436295","@uri":"xyz/v1/Households/11111"}, 
          "person": {"@id":"2222222","@uri":"xyz/v1/People/22222222"}, 
          "communicationType": {"@id":"2","@uri":"xyz/v1/Communications/CommunicationTypes/2","name":"Work Phone"}, 
          "communicationGeneralType":"Telephone","communicationValue":"11111","searchCommunicationValue":"789787987","listed":"false", 
           "communicationComment":null,"createdDate":"2009-08-09T15:49:27","lastUpdatedDate":"2009-08-11T23:40:02"}, 
          {"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/11111","household": {"@id":"1111","@uri":"xyz/v1/Households/1111"}, 
          "person":{"@id":"244404","@uri":"xyz/v1/People/1111"}, 
          "communicationType":{"@id":"3","@uri":"xyz/v1/Communications/CommunicationTypes/3","name":"Mobile"}, 
          "communicationGeneralType":"Telephone","communicationValue":"22222","searchCommunicationValue":"5475454","listed":"true", 
           "communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"}, 
          {"@array":"true","@id":"15454","@uri":"xyz/v1/Communications/111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/1111"}, 
          "person":{"@id":"244444474","@uri":"xyz/v1/People/111111"}, 
          "communicationType":{"@id":"4","@uri":"xyz/v1/Communications/CommunicationTypes/4","name":"Email"}, 
          "communicationGeneralType":"Email","communicationValue":"[email protected]","searchCommunicationValue":"[email protected]","listed":"true", 
          "communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:39:06"} 
         ] 
         } 
         }'; 


    $json_decoded = json_decode($json_object); 
    echo "email: ".$json_decoded->communications->communication[3]->communicationValue."<br />"; 
+0

यह बहुत अच्छा है! यह पूरी तरह से काम करता है। धन्यवाद! – pgtips

14

ध्यान में रखकर आप json_decode घ आप डेटा इस तरह से है:

$data = json_decode($json); 

आप उपयोग कर सकते हैं var_dump(अच्छी तरह से, यह अगर Xdebug विस्तार, जिस पर अच्छा है के साथ प्रयोग किया उत्पादन रास्ता बेहतर लग रहा है है एक विकास मशीन, Btw) को पता है कि आपके डेटा में है:

// Allows you to know what's in the data ;-) 
var_dump($data); 
object(stdClass)[1] 
    public 'communications' => 
    object(stdClass)[2] 
     public 'communication' => 
     array 
      0 => 
      object(stdClass)[3] 
       public '@array' => string 'true' (length=4) 
       public '@id' => string '23101384' (length=8) 
       public '@uri' => string 'xyz/v1/Communications/1111' (length=26) 
       public 'household' => 
       object(stdClass)[4] 
        public '@id' => string '111111' (length=6) 
        public '@uri' => string 'xyz/v1/Households/5465465' (length=25) 
       public 'person' => 
       object(stdClass)[5] 
        public '@id' => string '' (length=0) 
        public '@uri' => string '' (length=0) 
       public 'communicationType' => 
       object(stdClass)[6] 
        public '@id' => string '1' (length=1) 
        public '@uri' => string 'xyz/v1/Communications/CommunicationTypes/1' (length=42) 
        public 'name' => string 'Home Phone' (length=10) 
       public 'communicationGeneralType' => string 'Telephone' (length=9) 
       public 'communicationValue' => string '1111' (length=4) 
       public 'searchCommunicationValue' => string '2693240758' (length=10) 
       public 'listed' => string 'true' (length=4) 
       public 'communicationComment' => null 
       public 'createdDate' => string '2008-11-10T12:31:26' (length=19) 
       public 'lastUpdatedDate' => string '2009-08-11T23:40:02' (length=19) 
      1 => 
      object(stdClass)[7] 
       public '@array' => string 'true' (length=4) 
       public '@id' => string '11111' (length=5) 
       public '@uri' => string 'xyz/v1/Communications/111111111' (length=31) 
       public 'household' => 
       object(stdClass)[8] 
        public '@id' => string '14436295' (length=8) 
        public '@uri' => string 'xyz/v1/Households/11111' (length=23) 
       public 'person' => 
       object(stdClass)[9] 
        public '@id' => string '2222222' (length=7) 
        public '@uri' => string 'xyz/v1/People/22222222' (length=22) 
       public 'communicationType' => 
       object(stdClass)[10] 
        public '@id' => string '2' (length=1) 
        public '@uri' => string 'xyz/v1/Communications/CommunicationTypes/2' (length=42) 
        public 'name' => string 'Work Phone' (length=10) 
       public 'communicationGeneralType' => string 'Telephone' (length=9) 
       public 'communicationValue' => string '11111' (length=5) 
       public 'searchCommunicationValue' => string '789787987' (length=9) 
       public 'listed' => string 'false' (length=5) 
       public 'communicationComment' => null 
       public 'createdDate' => string '2009-08-09T15:49:27' (length=19) 
       public 'lastUpdatedDate' => string '2009-08-11T23:40:02' (length=19) 
      2 => 
      object(stdClass)[11] 
       public '@array' => string 'true' (length=4) 
       public '@id' => string '11111' (length=5) 
       public '@uri' => string 'xyz/v1/Communications/11111' (length=27) 
       public 'household' => 
       object(stdClass)[12] 
        public '@id' => string '1111' (length=4) 
        public '@uri' => string 'xyz/v1/Households/1111' (length=22) 
       public 'person' => 
       object(stdClass)[13] 
        public '@id' => string '244404' (length=6) 
        public '@uri' => string 'xyz/v1/People/1111' (length=18) 
       public 'communicationType' => 
       object(stdClass)[14] 
        public '@id' => string '3' (length=1) 
        public '@uri' => string 'xyz/v1/Communications/CommunicationTypes/3' (length=42) 
        public 'name' => string 'Mobile' (length=6) 
       public 'communicationGeneralType' => string 'Telephone' (length=9) 
       public 'communicationValue' => string '22222' (length=5) 
       public 'searchCommunicationValue' => string '5475454' (length=7) 
       public 'listed' => string 'true' (length=4) 
       public 'communicationComment' => null 
       public 'createdDate' => string '2008-11-10T12:31:26' (length=19) 
       public 'lastUpdatedDate' => string '2009-08-11T23:40:02' (length=19) 
      3 => 
      object(stdClass)[15] 
       public '@array' => string 'true' (length=4) 
       public '@id' => string '15454' (length=5) 
       public '@uri' => string 'xyz/v1/Communications/111111' (length=28) 
       public 'household' => 
       object(stdClass)[16] 
        public '@id' => string '14436295' (length=8) 
        public '@uri' => string 'xyz/v1/Households/1111' (length=22) 
       public 'person' => 
       object(stdClass)[17] 
        public '@id' => string '244444474' (length=9) 
        public '@uri' => string 'xyz/v1/People/111111' (length=20) 
       public 'communicationType' => 
       object(stdClass)[18] 
        public '@id' => string '4' (length=1) 
        public '@uri' => string 'xyz/v1/Communications/CommunicationTypes/4' (length=42) 
        public 'name' => string 'Email' (length=5) 
       public 'communicationGeneralType' => string 'Email' (length=5) 
       public 'communicationValue' => string '[email protected]' (length=18) 
       public 'searchCommunicationValue' => string '[email protected]' (length=18) 
       public 'listed' => string 'true' (length=4) 
       public 'communicationComment' => null 
       public 'createdDate' => string '2008-11-10T12:31:26' (length=19) 
       public 'lastUpdatedDate' => string '2009-08-11T23:39:06' (length=19) 

यदि आप स्वयं कुछ इस तरह से अपनी वांछित उपयोग करने में सक्षम होना चाहिए जिसका मतलब है: 210 आप कुछ इस तरह मिल जाएगा

foreach ($data->communications->communication as $communication) { 
    if ($communication->communicationGeneralType == 'Email') { 
     var_dump($communication->communicationValue); 
     var_dump($communication->searchCommunicationValue); 
    } 
} 

जो तुम मिल जाएगा:

string '[email protected]' (length=18) 
string '[email protected]' (length=18) 

"communications" एक वस्तु है कि "communication" शामिल है, जो वस्तुओं की एक सरणी, हर एक एक communicationGeneralType युक्त है, जो आपको यह निर्धारित करने की अनुमति देता है चाहे वर्तमान संचार एक ईमेल है या नहीं।

यह कहां है, आप communicationValue या searchCommunicationValue फ़ील्ड का उपयोग कर सकते हैं।

और मैं वास्तव में हर communication तत्व पर पुनरावृत्ति के बिना ऐसा करने का एक तरीका नहीं दिख रहा है ...

आशा इस मदद करता है!

+2

शानदार!Xdebug के लिए +1! – Swanand

+0

बहुत उपयोगी, धन्यवाद! – pgtips

6

आप json_decode() का उपयोग कर सकते हैं। आपका उदाहरण स्ट्रिंग थोड़ा जटिल मेरे बारे में सोचने के लिए के लिए अभी वहाँ है, लेकिन मेरे अपने का एक उदाहरण के रूप में:

$json = '{"a":"apples","b":["bananas","boysenberries"],"c":"carrots"}'; 

$arr = json_decode($json); 
echo $arr['a']; // "apples" 
echo $arr['b'][0]; // "bananas" 
+0

हां यह वही है जो मैं चाहता हूं लेकिन मैं विशिष्टताओं को समझ नहीं सकता? किसी भी प्रकार की मदद की बेहद सराहना की जाती है। – pgtips

+0

कौन सा हिस्सा समस्या है? – nickf

+0

ईमेल पते के लिए सरणी नाम और कुंजी क्या होगी? – pgtips

0
<?php 
$string = '{"communications":{"communication":[{"@array":"true","@id":"23101384","@uri":"xyz/v1/Communications/1111","household":{"@id":"111111","@uri":"xyz/v1/Households/5465465"},"person":{"@id":"","@uri":""},"communicationType":{"@id":"1","@uri":"xyz/v1/Communications/CommunicationTypes/1","name":"Home Phone"},"communicationGeneralType":"Telephone","communicationValue":"1111","searchCommunicationValue":"2693240758","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/111111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/11111"},"person":{"@id":"2222222","@uri":"xyz/v1/People/22222222"},"communicationType":{"@id":"2","@uri":"xyz/v1/Communications/CommunicationTypes/2","name":"Work Phone"},"communicationGeneralType":"Telephone","communicationValue":"11111","searchCommunicationValue":"789787987","listed":"false","communicationComment":null,"createdDate":"2009-08-09T15:49:27","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"11111","@uri":"xyz/v1/Communications/11111","household":{"@id":"1111","@uri":"xyz/v1/Households/1111"},"person":{"@id":"244404","@uri":"xyz/v1/People/1111"},"communicationType":{"@id":"3","@uri":"xyz/v1/Communications/CommunicationTypes/3","name":"Mobile"},"communicationGeneralType":"Telephone","communicationValue":"22222","searchCommunicationValue":"5475454","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:40:02"},{"@array":"true","@id":"15454","@uri":"xyz/v1/Communications/111111","household":{"@id":"14436295","@uri":"xyz/v1/Households/1111"},"person":{"@id":"244444474","@uri":"xyz/v1/People/111111"},"communicationType":{"@id":"4","@uri":"xyz/v1/Communications/CommunicationTypes/4","name":"Email"},"communicationGeneralType":"Email","communicationValue":"[email protected]","searchCommunicationValue":"[email protected]","listed":"true","communicationComment":null,"createdDate":"2008-11-10T12:31:26","lastUpdatedDate":"2009-08-11T23:39:06"}]}}'; 

$encoded = json_decode($string, JSON_FORCE_OBJECT); 

echo $encoded['communications']['communication'][3]['communicationValue']; 

echo "\n"; 

echo $encoded['communications']['communication'][3]['searchCommunicationValue']; 

?> 

लेकिन communicationValue या searchCommunicationValue कभी communication के सूचकांक 3 से बाहर निकलना है, तो आप ' मुसीबत में फिर से आपको शायद communication लूप करना होगा और इन तारों को इसकी कुंजी/मानों पर खोजें।

0

अच्छा उदाहरण मुझे

द्वारा लिखित JSON_OBJECT और 1 समारोह में दर्रा, आप प्रति संरचना के रूप में सभी मूल्यों को देखेंगे ।

function decodejson($value,$num){ 
    if (count($value,0) > 0 && is_array($value)){ 
     foreach ($value as $key =>$tvalue){ 
      if (is_array($tvalue)){ 
       //echo $key."-<br />"; 
       $num++; 
       decodejson($tvalue,$num); 
      }else 
       echo str_repeat("&nbsp;", $num).$key."->".$tvalue."<br />"; 
     } 
    }else 
     echo str_repeat("&nbsp;", $num).$key."->".$value."<br />"; 
} 
संबंधित मुद्दे