Iam prova con json_encoding per circa due ore ma non riesce a ottenere l'output come richiesto. In realtà questo è un requisito per lo sviluppatore di applicazioni mobili che chiede nel formato che vi spiegherò codice here.The sotto è quello che ho provato:Dati json_encoding PHP con categorie padre e figlio
include_once("class_connection.php");
//Getting the Parent Category
$sqlStr = mysql_query("select catname , id from `category` where `parentid`='0'");
$jsonArray = array();
while ($fetchStr = mysql_fetch_assoc($sqlStr)) {
$jsonArray[] = array("ParentCategory" => $fetchStr["catname"]);
$id = $fetchStr['id'];
//Getting child categories from the above parent
$sqlChildStr = mysql_query("SELECT catname,id,parentid FROM `category` where `parentid`='$id'");
while ($fetchchildStr = mysql_fetch_assoc($sqlChildStr)) {
$jsonArray[] = array("ChildCategory" => $fetchchildStr["catname"]);
}
}
echo json_encode(array("JsonOutput" => $jsonArray)) . "<br />";
l'output è:
"JsonOutput":[{"ParentCategory":"Animals"},{"ChildCategory":"Bear"},{"ChildCategory":"Deer"},{"ChildCategory":"Dolphins"},
{"ParentCategory":"Art"},{"ChildCategory":"Hand Painting"},{"ChildCategory":"Painting"},{"ChildCategory":"3D"},{"ChildCategory":"Abstract"}]}
Qui , nell'output precedente l'array della categoria padre è vuoto senza il relativo array di categorie figlio. Voglio memorizzare tutte le categoria di matrice bambino nella sua gamma categoria principale e, infine, devo memorizzare sia genitore e bambini della categoria nella matrice JsonOutput quindi voglio l'uscita come
"JsonOutput":[{
"ParentCategory":"Animals" : [{
{"ChildCategory":"Bear"},{"ChildCategory":"Deer"},{"ChildCategory":"Dolphins"}
]}
"ParentCategory":"Arts" : [{
{"ChildCategory":"Hand Painting"},{"ChildCategory":"Painting"},{"ChildCategory":"3D"}, {"ChildCategory":"Abstract"}
]}
]}
L'output desiderato non mi sembra JSON valido. –
Sembra che tu stia cercando di formattare (o migliorare) il tuo output JSON. Ma perché è necessario in primo luogo? Gli umani lo processeranno? –
@Pekka: guarda più da vicino, non è necessariamente una questione di prettificazione: '" ParentCategory ":" Animals ": [{' <- solo per creare JSON non valido. – hakre