2012-01-13 10 views
41

से JSON बारे में मैं एक फ़ाइल list.txt में JSON निम्नलिखित है:पीएचपी पढ़ सकते हैं और फ़ाइल

{ 
"bgates":{"first":"Bill","last":"Gates"}, 
"sjobs":{"first":"Steve","last":"Jobs"} 
} 

मैं अपने फ़ाइल PHP का उपयोग करने के लिए "bross":{"first":"Bob","last":"Ross"} जोड़ सकता हूँ?

यहाँ मैं अब तक है:

<?php 

$user = "bross"; 
$first = "Bob"; 
$last = "Ross"; 

$file = "list.txt"; 

$json = json_decode(file_get_contents($file)); 

$json[$user] = array("first" => $first, "last" => $last); 

file_put_contents($file, json_encode($json)); 

?> 

कौन सा मुझे एक गंभीर त्रुटि देता है: इस लाइन पर सरणी के रूप में प्रकार stdClass की वस्तु का उपयोग नहीं कर सकता:

$json[$user] = array("first" => $first, "last" => $last); 

मैं PHP5 उपयोग कर रहा हूँ। 2। कोई विचार? धन्यवाद!

उत्तर

77

सुराग त्रुटि संदेश में है - यदि आप json_decode के लिए प्रलेखन को देखते हैं तो यह ध्यान रखें कि यह एक दूसरा पैरा ले सकता है, जो नियंत्रित करता है कि यह एक सरणी या वस्तु देता है - यह ऑब्जेक्ट पर डिफ़ॉल्ट होता है।

तो

$json = json_decode(file_get_contents($file), true); 

करने के लिए अपने कॉल को बदलने और यह एक साहचर्य सरणी वापस आ जाएंगे, और अपने कोड ठीक काम करना चाहिए।

+3

मुझे डिफ़ॉल्ट रूप से 'json_decode' से नफरत है कि एक सरणी के बजाय कक्षा को लौटाता है। यह हर बार जब मैं एक महीने में पहली बार 'json_decode' का उपयोग करता हूं तो मुझे जाता है। – Tim

7

आपको true पैरामीटर में पास करके डीकोड फ़ंक्शन एक सरणी वापस करने की आवश्यकता है।

json_decode(file_get_contents($file),true);

3

json_decode समारोह के लिए दूसरा पैरामीटर उपयोग करके देखें:

$json = json_decode(file_get_contents($file), true); 
8

या सिर्फ एक वस्तु के रूप में $ json का उपयोग करें:

$json->$user = array("first" => $first, "last" => $last); 

यह है कि यह कैसे दूसरा पैरामीटर के बिना दिया जाता है है (stdClass के उदाहरण के रूप में)।

3

यह आपको पढ़ने के लिए list.txt फ़ाइल

$headers = array('http'=>array('method'=>'GET','header'=>'Content: type=application/json \r\n'.'$agent \r\n'.'$hash')); 

$context=stream_context_create($headers); 

$str = file_get_contents("list.txt",FILE_USE_INCLUDE_PATH,$context); 

$str=utf8_encode($str); 

$str=json_decode($str,true); 

print_r($str); 
+0

मुझे सिर्फ –

18

नमूना की सामग्री को मिलता है और करने के लिए काम करना चाहिए PHP में JSON लेखन:

$json = json_decode(file_get_contents($file),TRUE); 

$json[$user] = array("first" => $first, "last" => $last); 

file_put_contents($file, json_encode($json)); 
+1

से मेरा आउटपुट के रूप में ऐरे मिल गया है json_encode का दूसरा तर्क एक बूलियन नहीं है, इसलिए 'json_encode ($ json, TRUE) लिखना गलत है। –

+0

दस्तावेज़ों के अनुसार (http://php.net/manual/en/function.json-decode.php) दूसरा तर्क एक बूलियन है। –

1

आप json प्रारूप यह करना पड़ा बनानी हो तो इसे पढ़ने के लिए इस प्रारूप में रहें:

[ 
    { 
    "":"", 
    "":[ 
    { 
     "":"", 
     "":"" 
    } 
    ] 
    } 
] 
2

यदि आप JSON डेटा को अच्छी तरह से प्रदर्शित करना चाहते हैं परिभाषित फॉर्मेट के रूप में आप कोड को संशोधित कर सकते हैं:

file_put_contents($file, json_encode($json,TRUE)); 


$headers = array('http'=>array('method'=>'GET','header'=>'Content: type=application/json \r\n'.'$agent \r\n'.'$hash')); 

$context=stream_context_create($headers); 

$str = file_get_contents("list.txt",FILE_USE_INCLUDE_PATH,$context); 

$str1=utf8_encode($str); 

$str1=json_decode($str1,true); 



foreach($str1 as $key=>$value) 
{ 

    echo "key is: $key.\n"; 

    echo "values are: \t"; 
     foreach ($value as $k) { 

     echo " $k. \t"; 
     # code... 
    } 
     echo "<br></br>"; 
     echo "\n"; 

} 
संबंधित मुद्दे