0

i want to get the specific value from the json, but i can't get it to work. this is my save format, these are values from input fields.

               $.ajax({
              type: "POST",
              url: "speichern.php",
              dataType: 'json',
              data:  {
                    "Produkt": {
                        "Produktkategorie": Produktkategorie,
                         "Optionen": {
                            "MaxBreite": MaxBreite,
                            "MaxHoehe": MaxHoehe, 
                            "MinBreite": MinBreite,
                            "MinHoehe": MinHoehe, 
                            "ProduktStaerke": ProduktStaerke,
                            "KantenAuswahl": KantenAuswahl, },                               
                                "Formen": {
                                    "FormRund": FormRund,
                                    "FormEllipse": FormEllipse,
                                    "FormHexagon": FormHexagon,
                                    "FormSchnittlinks": FormSchnittlinks,
                                    "FormRechtQuad": FormRechtQuad,
                                }


                    }
                },
            }).done(function( msg ) {
              console.log( msg );
            }); 

here it gets saved to file:

$neu = json_encode($_POST);
$file = file_get_contents('results.json');
$data = json_decode($file);
unset($file);
$data[] = $neu;
file_put_contents('results.json',json_encode($data));
unset($data);

and now i want to echo these values seperately:

$string = file_get_contents("results.json");
$jsonObject = json_decode($string);
$jsonArray = json_decode($string, true); 
echo $jsonObject->Produkt->Produktkategorie . " and " . `$jsonArray['Produkt']['MaxBreite'];`

but this only throws me following errors:

for the object: Notice: Trying to get property of non-object in for the array: Notice: Undefined index: Produkt in

this is the complete json file:

["{\"Produkt\":{\"Produktkategorie\":\"TestArtikel\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Ecke\"},\"Formen\":{\"FormRund\":\"true\",\"FormEllipse\":\"true\",\"FormRechtQuad\":\"true\"}}}"]

could you help me please?

chim
  • 41
  • 2
  • 9

3 Answers3

1

When you posting the data, maybe you have to set the datatype.
dataType: 'json'

      $.ajax({
        url: 'speichern.php',
        type: 'post',
        dataType: 'json',
        success: function (data) {

        },
        data: {data:jsondata}
    });  

And in your php file, you could get the json data like following.

$json=json_decode(stripslashes($_POST['data']), true);

Hope it helps you.

Andrew Li
  • 1,005
  • 12
  • 29
0

Simply replace your last line with

echo $jsonObject->Produkt->Produktkategorie . " and " . `$jsonArray['Produkt']['Optionen']['MaxBreite'];
syed
  • 289
  • 1
  • 5
  • 17
0

You need to decode the json two times because the way you have it in the file. Try this:

$json = file_get_contents('results.json');

$json = json_decode($json, true);
$json = json_decode($json[0], true);
echo $json['Produkt']['Produktkategorie'];
Ionut Necula
  • 11,107
  • 4
  • 45
  • 69