Ankündigung

Einklappen
Keine Ankündigung bisher.

Json Parse mit PHP extended

Einklappen

Neue Werbung 2019

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

  • Json Parse mit PHP extended

    Hallo,

    ich bräuchte mal die Hilfe der Community.
    Ich habe folgendes Jsonfile:
    Code:
    [
      {
        "uri": "features/complete_ski.feature",
        "id": "complete_ski_with_time",
        "keyword": "Feature",
        "name": "Complete_Ski_with_time",
        "description": "",
        "line": 1,
        "elements": [
          {
            "id": "complete_ski_with_time;time_part_preamble",
            "keyword": "Scenario",
            "name": "time_part_preamble",
            "description": "",
            "line": 3,
            "type": "scenario",
            "before": [
              {
                "output": [
                  "Default Timestamp start: 1516024716000"
                ],
                "match": {
                  "location": "features/support/env.rb:32"
                },
                "result": {
                  "status": "passed",
                  "duration": 191690
                }
              },
              {
                "match": {
                  "location": "capybara-2.17.0/lib/capybara/cucumber.rb:13"
                },
                "result": {
                  "status": "passed",
                  "duration": 52117
                }
              },
              {
                "match": {
                  "location": "capybara-2.17.0/lib/capybara/cucumber.rb:21"
                },
                "result": {
                  "status": "passed",
                  "duration": 25885
                }
              }
            ],
            "steps": [
              {
                "keyword": "Given ",
                "name": "a Android A-Party",
                "line": 4,
                "output": [
                  "Got handset with number unvisable, IMSI: notfor, android-Id: yourfone, VNC: 11111, port: 9981"
                ],
                "match": {
                  "location": "features/step_definitions/idrp_steps.rb:11"
                },
                "result": {
                  "status": "passed",
                  "duration": 1415024760
                },
                "after": [
                  {
                    "match": {
                      "location": "features/support/env.rb:24"
                    },
                    "result": {
                      "status": "passed",
                      "duration": 264339
                    }
                  }
                ]
              }
              ..................................................................
    Ich möchte nun zum Beispiel den Status, die Timestamps, den String "Got handset with number......" etc. einzeln in eine Variable speichern. Ich habe schon mit folgenden PHP Code angefangen:
    PHP-Code:
    <?php
       
    // phpinfo();
       
    $jsonconntent file_get_contents("/home/maxi/test1.json");
       
    $data json_decode($jsonconntenttrue);
       foreach ( 
    $data as $item ) {
        
    $Testcasename $item['name'];
          foreach ( 
    $item['elements'] as $element ) {
           
    // not possible $timestamp1 = $element['output'];
           
    $type $element['keyword'];
           foreach (
    $element['before'] as $element1) {
    }
       }
       }
    ?>

  • #2
    Und was ist deine Frage? Der Schnipsel ist kein valides JSON.

    MOD: Verschoben von JS
    The string "()()" is not palindrom but the String "())(" is.

    Debugging: Finde DEINE Fehler selbst! | Gegen Probleme beim E-Mail-Versand | Sicheres Passwort-Hashing | Includes niemals ohne __DIR__
    PHP.de Wissenssammlung | Kein Support per PN

    Kommentar


    • #3
      Die Frage lautet wie ich da den Status, die Timestamps, den String "Got handset with number......" etc. einzeln in einen String speichern kann.
      Der Schnipsel ist auch nicht vollständig, weil ich nicht alles posten kann. Leider auf Grund von Datenschutz....

      Kommentar


      • #4
        Lass Dir doch einfach das entstandene Array mal anzeigen. Dann siehst Du die Struktur.
        PHP-Manual ¡ mysql_* ist veraltet ¡ Debugging: Finde DEINE Fehler selbst ¡ Passwort-Hashing ¡ Prepared Statements

        Kommentar


        • #5
          Habe ich doch gemacht mit var_dump.
          Allerdings sieht das total komisch aus.
          Code:
          array(1) { [0]=> array(7) { ["uri"]=> string(32) "features/complete_ski.feature" ["id"]=> string(29) "ski_with_timely_datavolume" ["keyword"]=> string(7) "Feature" ["name"]=> string(29) "ski_with_timely_datavolume" ["description"]=> string(0) "" ["line"]=> int(1) ["elements"]=> array(3) { [0]=> array(9) { ["id"]=> string(54) "ski_with_timely_datavolume;datavolume_part_preamble" ["keyword"]=> string(8) "Scenario" ["name"]=> string(24) "datavolume_part_preamble" ["description"]=> string(0) "" ["line"]=> int(3) ["type"]=> string(8) "scenario" ["before"]=> array(3) { [0]=> array(3) { ["output"]=> array(1) { [0]=> string(38) "Default Timestamp start: 1516024716000" } ["match"]=> array(1) { ["location"]=> string(26) "features/support/env.rb:32" } ["result"]=> array(2) { ["status"]=> string(6) "passed" ["duration"]=> int(191690) } } [1]=> array(2) { ["match"]=> array(1) { ["location"]=> string(43) "capybara-2.17.0/lib/capybara/cucumber.rb:13" } ["result"]=> array(2) { ["status"]=> string(6) "passed" ["duration"]=> int(52117) } } [2]=> array(2) { ["match"]=> array(1) { ["location"]=> string(43) "capybara-2.17.0/lib/capybara/cucumber.rb:21" } ["result"]=> array(2) { ["status"]=> string(6) "passed" ["duration"]=> int(25885) } } } ["steps"]=> array(5) { [0]=> array(7) { ["keyword"]=> string(6) "Given " ["name"]=> string(17) "a Android A-Party" ["line"]=> int(4) ["output"]=> array(1) { [0]=> string(114) "Got handset with number +sdfa, IMSI: sadf, android-Id: dsaf, VNC: 5904, port: 9515" } ["match"]=> array(1) { ["location"]=> string(42) "features/step_definitions/idrp_steps.rb:11" } ["result"]=> array(2) { ["status"]=> string(6) "passed" ["duration"]=> int(1415024760) } ["after"]=> array(1) { [0]=> array(2) { ["match"]=> array(1) { ["location"]=> string(26) "features/support/env.rb:24" } ["result"]=> array(2) { ["status"]=> string(6) "passed" ["duration"]=> int(264339) } } } } [1]=> array(7) { ["keyword"]=> string(5) "Then " ["name"]=> string(29) "I switch off the data session" ["line"]=> int(5) ["output"]=> array(1) { [0]=> string(40) "Successfully changed PDPContext to: off." } ["match"]=> array(1) { ["location"]=> string(42) "features/step_definitions/idrp_steps.rb:38" } ["result"]=> array(2) { ["status"]=> string(6) "passed" ["duration"]=> int(3312561373) } ["after"]=> array(1) { [0]=> array(2) { ["match"]=> array(1) { ["location"]=> string(26) "features/support/env.rb:24" } ["result"]=> array(2) { ["status"]=> string(6) "passed" ["duration"]=> int(2628398) } } } } [2]=> array(7) { ["keyword"]=> string(5) "Then " ["name"]=> string(28) "I switch on the data session" ["line"]=> int(6) ["output"]=> array(3) { [0]=> string(40) "Successfully changed PDPContext to: off."

          Kommentar


          • #6
            Nimm mal besser die Funktion dump_arrobj() hier aus der Wissenssammlung oder alternativ Debug (s.signatur)
            PHP-Code:
            function dump_arrobj($arrobj) {
                echo 
            '<pre>'.str_ireplace'stdClass::__set_state''(object)'var_export($arrobj,true) ).'</pre>';

            und versuche das Resultat hier im Forum mit Gross-Strg-V im PHP-Tag einzufügen, damit nicht alles in einer Zeile erscheint.

            Kommentar


            • #7
              Zitat von Maloe Beitrag anzeigen
              Habe ich doch gemacht mit var_dump.
              Allerdings sieht das total komisch aus.
              Das ist ja auch nur der Dump. Wenn du es hier mit STRG+SHIFT+V einfügst bleibt die Struktur erkennbar, die wäre wichtig um dir helfen zu können.
              The string "()()" is not palindrom but the String "())(" is.

              Debugging: Finde DEINE Fehler selbst! | Gegen Probleme beim E-Mail-Versand | Sicheres Passwort-Hashing | Includes niemals ohne __DIR__
              PHP.de Wissenssammlung | Kein Support per PN

              Kommentar


              • #8
                Also, ich habe das jetzt mit Array--Manipulation gemacht ohne Schleifen etc..

                Kommentar


                • #9
                  Ganz versteh ich es immer noch nicht, aber es ist gelöst?

                  Der Vollständigkeit halber .. angenommen, du hast folgendes valides JSON:

                  PHP-Code:
                  $json = <<<JSON
                  [{
                      "uri": "features/complete_ski.feature",
                      "id": "complete_ski_with_time",
                      "keyword": "Feature",
                      "name": "Complete_Ski_with_time",
                      "description": "",
                      "line": 1,
                      "elements": [{
                          "id": "complete_ski_with_time;time_part_preamble",
                          "keyword": "Scenario",
                          "name": "time_part_preamble",
                          "description": "",
                          "line": 3,
                          "type": "scenario",
                          "before": [{
                                  "output": [
                                      "Default Timestamp start: 1516024716000"
                                  ],
                                  "match": {
                                      "location": "features/support/env.rb:32"
                                  },
                                  "result": {
                                      "status": "passed",
                                      "duration": 191690
                                  }
                              },
                              {
                                  "match": {
                                      "location": "capybara-2.17.0/lib/capybara/cucumber.rb:13"
                                  },
                                  "result": {
                                      "status": "passed",
                                      "duration": 52117
                                  }
                              },
                              {
                                  "match": {
                                      "location": "capybara-2.17.0/lib/capybara/cucumber.rb:21"
                                  },
                                  "result": {
                                      "status": "passed",
                                      "duration": 25885
                                  }
                              }
                          ],
                          "steps": [{
                              "keyword": "Given ",
                              "name": "a Android A-Party",
                              "line": 4,
                              "output": [
                                  "Got handset with number unvisable, IMSI: notfor, android-Id: yourfone, VNC: 11111, port: 9981"
                              ],
                              "match": {
                                  "location": "features/step_definitions/idrp_steps.rb:11"
                              },
                              "result": {
                                  "status": "passed",
                                  "duration": 1415024760
                              },
                              "after": [{
                                  "match": {
                                      "location": "features/support/env.rb:24"
                                  },
                                  "result": {
                                      "status": "passed",
                                      "duration": 264339
                                  }
                              }]
                          }]
                      }]
                  }]
                  JSON; 
                  Dann bekommst du zB mit
                  PHP-Code:
                  $a json_decode($jsontrue);
                  print_r$a[0]['elements'][0]['steps'][0] );

                  /*
                  Array
                  (
                      [keyword] => Given
                      [name] => a Android A-Party
                      [line] => 4
                      [output] => Array
                          (
                              [0] => Got handset with number unvisable, IMSI: notfor, android-Id: yourfone, VNC: 11111, port: 9981
                          )

                      [match] => Array
                          (
                              [location] => features/step_definitions/idrp_steps.rb:11
                          )

                      [result] => Array
                          (
                              [status] => passed
                              [duration] => 1415024760
                          )

                      [after] => Array
                          (
                              [0] => Array
                                  (
                                      [match] => Array
                                          (
                                              [location] => features/support/env.rb:24
                                          )

                                      [result] => Array
                                          (
                                              [status] => passed
                                              [duration] => 264339
                                          )

                                  )

                          )

                  )

                  */ 
                  Und so weiter.
                  The string "()()" is not palindrom but the String "())(" is.

                  Debugging: Finde DEINE Fehler selbst! | Gegen Probleme beim E-Mail-Versand | Sicheres Passwort-Hashing | Includes niemals ohne __DIR__
                  PHP.de Wissenssammlung | Kein Support per PN

                  Kommentar


                  • #10
                    So sieht es auch aus. Deshalb ist es für mich gelöst.

                    Kommentar


                    • #11
                      ok, bitte nächstes mal auch die Lösung posten, das andere auch devon profitiern können. Hab ich hier ja nun schon gemacht.
                      The string "()()" is not palindrom but the String "())(" is.

                      Debugging: Finde DEINE Fehler selbst! | Gegen Probleme beim E-Mail-Versand | Sicheres Passwort-Hashing | Includes niemals ohne __DIR__
                      PHP.de Wissenssammlung | Kein Support per PN

                      Kommentar


                      • #12
                        Hi,

                        also ich habe nun eine Frage in Verbindung mit Schleifen.
                        Wenn ich das ganze dynamisch machen will, wie gehe ich da am besten vor.

                        Folgender Code existiert:

                        PHP-Code:
                                // Jsondatei in Array einlesen
                           
                        $jsonconntent file_get_contents("/home/maxi/test1.json");
                           
                        $data json_decode($jsonconntenttrue);

                                
                        // PreTestcase
                           
                        $nameTestcase $data[0]['name'];
                           
                        $nameTestcase1 $data[0]['elements'][0]['name'];
                           
                        $timestamp1 $data[0]['elements'][0]['before'][0]['output'][0];

                           
                        //1. Schritt
                           
                        $Step1name1 $data[0]['elements'][0]['steps'][0]['keyword'];
                           
                        $Step1name2 $data[0]['elements'][0]['steps'][0]['name'];
                           
                        $Step1name $Step1name1 $Step1name2;
                           
                        $Step1ausgabe implode("\n"$data[0]['elements'][0]['steps'][0]['output']);
                           
                        $Step1ergebnis $data[0]['elements'][0]['steps'][0]['result']['status'];
                           
                        $Step1dauer $data[0]['elements'][0]['steps'][0]['result']['duration']; 
                        Was ich nun vorhab ist folgendes:
                        PHP-Code:
                        While($data[0]['elements'][0]['steps'][xexistiert){
                           
                        $Stepxname1 $data[0]['elements'][0]['steps'][x]['keyword'];
                           
                        $Stepxname2 $data[0]['elements'][0]['steps'][x]['name'];
                           
                        $Stepxname $Step1name1 $Step1name2;
                           
                        $Stepxausgabe implode("\n"$data[0]['elements'][x]['steps'][0]['output']);
                           
                        $Stepxergebnis $data[0]['elements'][0]['steps'][x]['result']['status'];
                           
                        $Stepxdauer $data[0]['elements'][0]['steps'][x]['result']['duration'];

                        Also solange der Arraywert exisiert soll der jeweilige Arraywert in den Variablen angepasst und gespeichert werden. Also wenn [x] existiert, soll mit [x1] weitergemacht werden. So dass ich ein dynamisches Skript habe und es nicht für jedes unters. Array anpassen muss.
                        Wie macht man sowas am Besten? Da bin ich ehrlich überfragt.

                        Kommentar


                        • #13
                          Ich schätze mal mit einer Hilfsvariable die zählt sollte es doch funktionieren.

                          Kommentar


                          • #14
                            die foreach-schleife sollte dir helfen: http://php.net/manual/de/control-structures.foreach.php

                            PHP-Code:
                            foreach($data[0]['elements'][0]['steps'] as $key => $value) { echo $value; } 

                            Kommentar


                            • #15
                              Und mit $value ersetze ich dann alle [x] in meinem Statement? Wenn ich das mache, passiert aber nichts.
                              Folgender Code:
                              PHP-Code:
                              $jsonconntent file_get_contents("/home/maxi/test1.json");
                              $data json_decode($jsonconntenttrue);

                              foreach(
                              $data[0]['elements'][0]['steps'] as $key => $value) {
                                  
                              $test[$value] = $data[0]['elements'][0]['steps'][$value];
                              #    echo $value;

                              Ziel ist, dass dann folgendes solange geschrieben wird in die php Datei, bis es keine Steps mehr gibt (Ende des Arrays).

                              PHP-Code:
                                  $test[1] = $data[0]['elements'][0]['steps'][0];
                                  
                              $test[2] = $data[0]['elements'][0]['steps'][1];
                                  
                              $test[3] = $data[0]['elements'][0]['steps'][2];  
                                  
                              # Bei dem Beispiel (Ende des Arrays nach 3 Keys) 
                              Error:
                              Code:
                              PHP Warning:  Illegal offset type in /root/PhpstormProjects/JsonToXML/output.php on line 8
                              PHP Warning:  Illegal offset type in /root/PhpstormProjects/JsonToXML/output.php on line 8
                              PHP Notice:  Array to string conversion in /root/PhpstormProjects/JsonToXML/output.php on line 9
                              ArrayPHP Warning:  Illegal offset type in /root/PhpstormProjects/JsonToXML/output.php on line 8
                              PHP Warning:  Illegal offset type in /root/PhpstormProjects/JsonToXML/output.php on line 8
                              PHP Notice:  Array to string conversion in /root/PhpstormProjects/JsonToXML/output.php on line 9
                              ArrayPHP Warning:  Illegal offset type in /root/PhpstormProjects/JsonToXML/output.php on line 8
                              PHP Warning:  Illegal offset type in /root/PhpstormProjects/JsonToXML/output.php on line 8
                              PHP Notice:  Array to string conversion in /root/PhpstormProjects/JsonToXML/output.php on line 9
                              ArrayPHP Warning:  Illegal offset type in /root/PhpstormProjects/JsonToXML/output.php on line 8
                              PHP Warning:  Illegal offset type in /root/PhpstormProjects/JsonToXML/output.php on line 8
                              PHP Notice:  Array to string conversion in /root/PhpstormProjects/JsonToXML/output.php on line 9
                              ArrayPHP Warning:  Illegal offset type in /root/PhpstormProjects/JsonToXML/output.php on line 8
                              PHP Warning:  Illegal offset type in /root/PhpstormProjects/JsonToXML/output.php on line 8
                              PHP Notice:  Array to string conversion in /root/PhpstormProjects/JsonToXML/output.php on line 9

                              Kommentar

                              Lädt...
                              X