मैं इस समय PHP द्वारा JSON के साथ काम कर रहा हूँ, जब मैं यह सांकेतिक शब्दों में बदलना है, यह आउटपुट के रूप में होगा:संरचित JSON लेआउट
{
"username": "ND",
"email": "[email protected]",
"regdate": "8th June 2010",
"other":
{
"alternative": "ND"
},
"level":"6"
}
:
{"username":"ND","email":"[email protected]","regdate":"8th June 2010","other":{"alternative":"ND"},"level":"6"}
जब मैं यह उत्पादन के लिए इस तरह चाहते हैं
ताकि जब मैं और मेरे अन्य डेवलपर इसे संरचित करते समय पर्याप्त रूप से पढ़ सकें। मैं यह कैसे कर सकता हूँ?
भी इस तरह उदाहरण:
<?php
function json_pretty_encode($obj)
{
$json = json_encode($obj);
if (!$json) return $json;
$f = '';
$len = strlen($json);
$depth = 0;
$newline = false;
for ($i = 0; $i < $len; ++$i)
{
if ($newline)
{
$f .= "\n";
$f .= str_repeat(' ', $depth);
$newline = false;
}
$c = $json[$i];
if ($c == '{' || $c == '[')
{
$f .= $c;
$depth++;
$newline = true;
}
else if ($c == '}' || $c == ']')
{
$depth--;
$f .= "\n";
$f .= str_repeat(' ', $depth);
$f .= $c;
}
else if ($c == '"')
{
$s = $i;
do {
$c = $json[++$i];
if ($c == '\\')
{
$i += 2;
$c = $json[$i];
}
} while ($c != '"');
$f .= substr($json, $s, $i-$s+1);
}
else if ($c == ':')
{
$f .= ': ';
}
else if ($c == ',')
{
$f .= ',';
$newline = true;
}
else
{
$f .= $c;
}
}
return $f;
}
?>
यह है:
चीयर्स
मैं आउटपुट के लिए प्रारूपित करने की कोशिश करने के बजाय डेटा देखने के लिए बेहतर टूल का उपयोग करने की सलाह देता हूं। https://addons.mozilla.org/en-US/firefox/addon/10869/ – Quentin
@ डेविड के साथ सहमत हुए। फायरबग आपको जेएसओएन को पेड़ के रूप में भी नेविगेट करने देता है। बहुत उपयोगी। –
क्या आप इसे डीकोड करने से पहले json_encode() के साथ पहले से ही अच्छी तरह से गठित JSON को एन्कोड करने का प्रयास कर रहे हैं, क्योंकि इससे ऊपर का परिणाम सामने आएगा? - नीचे मेरा संपादन देखें। –