1

結果を適切に機能しないjquery getJSON関数に渡すこのphpコードがあります。

ここに私のコードがあります:

    $data["total"]=mysql_num_rows($s);
    while ($r = mysql_fetch_array($s))
    {

        $ris_sql["utente"]=tv($r["utente"]);
        $ris_sql["contratto"]=tv($r["contratto"]);
        $ris_sql["login"]=tv($r["login"]);
        $ris_sql["piattaforma"]=tv($r["piattaforma"]);
        $ris_sql["azione"]=tv(format_valid($r["azione"]));
        $ris_sql["data"]=tv($r["data"]);
        $ris_sql["note"]=tv(str_replace("<br>","",$r["note"]));
        $riga[]=$ris_sql;
    }
    $data["rows"]=json_encode($riga, JSON_FORCE_OBJECT);
    echo json_encode($data);

firebug を使用しようとすると、JSON の要素が一連のオブジェクトではなく文字列のようになることがわかります。コードの何が問題なのですか??

4

1 に答える 1

0

$rigaJSONをダブルエンコードしています。ブラウザに送り返す配列$rigaの残りの部分をエンコードする前に、その部分を個別にエンコードしないでください。$data

// Instead of
$data["rows"] = json_encode($riga, JSON_FORCE_OBJECT);
// This double-encodes the contents of $data before outputting back to the browser or ajax call
echo json_encode($data);

// Just do:
echo json_encode($data, JSON_FORCE_OBJECT);

形式が期待どおりでない場合は、代わりに$riga配列からにキャストして、。objectなしで全体をエンコードできJSON_FORCE_OBJECTます。

// Cast $riga from an assoc array to an instance of stdClass
$data['rows'] = (object)$riga;
// And then JSON encode the whole $data
echo json_encode($data);
于 2012-04-20T18:32:04.157 に答える