1

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?

8
  • What happens when you try echoing print_r($jsonObject, 1); print_r($jsonArray, 1); Commented Jan 30, 2017 at 13:37
  • Are you sure that the json is actually saved to the file? Commented Jan 30, 2017 at 13:40
  • the following error: "Notice: Array to string conversion in" Commented Jan 30, 2017 at 13:41
  • yes i check it every time,, i delete before is saved again the whole file Commented Jan 30, 2017 at 13:41
  • @Ajaypayne i try also the print method, nothing shows up, if i do "print_r($jsonArray, true)" - i get the whole json data Commented Jan 30, 2017 at 13:47

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

In your speichern.php, $neu=json_decode(stripslashes($_POST['data']), true);
1

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'];

Comments

0

Simply replace your last line with

echo $jsonObject->Produkt->Produktkategorie . " and " . `$jsonArray['Produkt']['Optionen']['MaxBreite'];

1 Comment

i get the same error: Notice: Undefined index: Produkt in

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.