So erstellen Sie eine json-Datei in PHP

Ich bin benötigen, erstellen Sie eine JSON-Datei von Grund auf, die wie folgt Aussehen wird

{
 "results": {
  "course": "CC167",
  "books": {
   "book": [
    {
      "-id": "585457",
      "-title": "Beginning XNA 20 game programming : from novice to professional",
      "-isbn": "1590599241",
      "-borrowedcount": "16"
    },
    {
      "-id": "325421",
      "-title": "Red Hat Linux 6",
      "-isbn": "0201354373",
      "-borrowedcount": "17"
    },
    {
      "-id": "424317",
      "-title": "Beginner's guide to darkBASIC game programming",
      "-isbn": "1592000096",
      "-borrowedcount": "46"
    },
    {
      "-id": "437390",
      "-title": "Objects first with Java : a practical introduction using BlueJ",
      "-isbn": "0131249339",
      "-borrowedcount": "89"
    },
    {
      "-id": "511094",
      "-title": "Objects first with Java : a practical introduction using BlueJ",
      "-isbn": "2006044765",
      "-borrowedcount": "169"
    }
   ]
  }
 }
}

Dies ist die PHP, die ich verwendet, um zu machen, also hoffentlich ist es nicht ein großer Sprung, aber ich kann einfach nicht alles auf, wie man die JSON-Objekte in PHP von Grund auf, nur wie man sowas dann speichern Sie es als ein JSON-selbst

$y = 1;
    $json = "{";
    $json = $json . "\"results\": {";
    $json = $json . "\"course\": \"$cc\",";
    $json = $json . "\"books\": {";
    $json = $json . "\"book\": [";
    foreach ($my_array as $counter => $bc) {
        $json = $json . "{";
        $json = $json . "\"-id\": \"$id[$counter]\",";
        $json = $json . "\"-title\": \"$title[$counter]\",";
        $json = $json . "\"-isbn\": \"$isbn[$counter]\",";
        $json = $json . "\"-borrowedcount\": \"$borrowedcount[$counter]\"";
        $json = $json . "}";
        if ($x != $y) $json = $json .  ",";
        $json = $json . "";
        $y++;
    }
    $json = $json . "]";
    $json = $json . "}";
    $json = $json . "}";
    $json = $json . "}";
    echo $json;
  • Wo ist die PHP, die Sie verwendet, um es zu schaffen? Wie für die Erstellung von JSON in PHP Aussehen at json_encode.
  • Sie don T make 'JSON Objekte zu manipulieren und diejenigen, die Sie die Ausgabe der Daten als JSON.
  • die Funktionen json_encode() nur google ...
  • wir hatten usenet-news-Gruppen, bbs und musste dann versuchen und scheitern , nochmal versuchen etc... 🙂
  • Welche PHP version verwenden Sie? Sie nicht zu verwenden, die anstößig syntax, wenn json_* erhältlich sind.
InformationsquelleAutor Craig Weston | 2013-02-25
Schreibe einen Kommentar