json Request aus PHP Array erstellen

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • json Request aus PHP Array erstellen

    Hi

    Ich erstelle einen PHP Array welchen ich nachher mit json_encode in eine json Request erstelle. Soweit so gut. Jedoch habe ich bei einem Feld mühe.

    Hier ein teilarray:
    PHP-Code:
    $pictureArray = array();
    //Mittels Schleife wird dann mehrfach der Array gefüllt:
    $pictureArray[] = array(
    "PictureBytes" => "....",
    "PictureIndex" => $i,
    "PicturePosition" => $position
    ); 
    Daraus erhalte ich folgenden request:
    Code:
    "Pictures": [
    {
         "PictureBytes": "",
         "PictureIndex": 1,
         "PicturePosition": 1
    },
    {
         "PictureBytes": "",
         "PictureIndex": 2,
         "PicturePosition": 5
    } 
    ]
    Ich brauche ihn jedoch ohne denn array [] also so:

    Code:
    "Pictures": {
         "PictureBytes": "",
         "PictureIndex": 1,
         "PicturePosition": 1
    },
    {
         "PictureBytes": "",
         "PictureIndex": 2,
         "PicturePosition": 5
    }
    Kann mir jemand weiterhelfen, wie ich zu meinem ergebnis komme? Irgendwie stehe ich da wohl auf dem schlauch oder übersehe etwas.

    Gruss und Danke

    Koda

  • #2
    Zitat von Koda Beitrag anzeigen
    Code:
    "Pictures": {
         "PictureBytes": "",
         "PictureIndex": 1,
         "PicturePosition": 1
    },
    {
         "PictureBytes": "",
         "PictureIndex": 2,
         "PicturePosition": 5
    }
    Das ist kein JSON. Ich kenn keine Funktion, die sowas kaputtes erzeugt. Musst du wohl per Hand machen.

    Kommentar


    • #3
      Ok vielen Dank
      Habe ich schon fast befürchtet

      Kommentar


      • #4
        Das ist sehr hässlich, aber es sollte tun, was du brauchst:
        PHP-Code:
        $placeholders = [];

        $generatePlaceholder = function () use (&$placeholders) {
            
        $placeholder md5(mt_rand());
            
        $placeholders[] = "\"" $placeholder "\": ";
            return 
        $placeholder;
        };

        $data = [
            
        "Pictures" => [
                
        "PictureBytes" => "",
                
        "PictureIndex" => 1,
                
        "PicturePosition" => 1
            
        ],
            
        $generatePlaceholder() => [
                
        "PictureBytes" => "",
                
        "PictureIndex" => 2,
                
        "PicturePosition" => 5
            
        ]
        ];

        header("Content-Type: text/plain"); // normalerweise application/json, aber es ist ja kein JSON
        echo str_replace($placeholders""json_encode($dataJSON_PRETTY_PRINT)); 
        Code:
        {
            "Pictures": {
                "PictureBytes": "",
                "PictureIndex": 1,
                "PicturePosition": 1
            },
            {
                "PictureBytes": "",
                "PictureIndex": 2,
                "PicturePosition": 5
            }
        }
        Zuletzt geändert von h3ll; 28.08.2015, 19:30.

        Kommentar

        Lädt...
        X