2012-12-14 9 views
5

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"} 
]} 
]} 
+3

L'output desiderato non mi sembra JSON valido. –

+4

Sembra che tu stia cercando di formattare (o migliorare) il tuo output JSON. Ma perché è necessario in primo luogo? Gli umani lo processeranno? –

+0

@Pekka: guarda più da vicino, non è necessariamente una questione di prettificazione: '" ParentCategory ":" Animals ": [{' <- solo per creare JSON non valido. – hakre

risposta

2

probabilmente bisogno di fare questo (solo i bit importanti sono mostrate):

$jsonArray = array(); 
while ($parentCat = mysql_fetch_assoc($sqlStr)) { 
    $temp = array(
     "ParentCategory" => $parentCat["catname"] 
    ); 
    while ($childCat = mysql_fetch_assoc($sqlChildStr)) { 
     $temp["ChildCategory"][] = array(
      "ChildCategory" => $childCat["catname"] 
     ); 
    } 
    $jsonArray[] = $temp; 
} 

ho usato una variabile temporanea per memorizzare e manipolando la categoria padre. Questo viene aggiunto alla matrice principale alla fine del ciclo.

+0

Grazie mille per l'aiuto. Funziona bene. – raduns

0

prega di utilizzare i seguenti codici, dare il indice all'interno del ciclo while ...

$jsonArray = {}; 
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["ParentCategory"][$fetchStr["catname"]] = array("ChildCategory" => $fetchchildStr["catname"]); 
    } 

} 

echo json_encode(array("JsonOutput" => $jsonArray)) . "<br />"; 
Problemi correlati