PHP Json回送特定项

PHP Json回送特定项,php,jquery,json,Php,Jquery,Json,我想从json中获取特定的值,但我无法让它工作。 这是我的保存格式,这些是输入字段中的值 $.ajax({ type: "POST", url: "speichern.php", dataType: 'json', data: { "Produkt": { "Prod

我想从json中获取特定的值,但我无法让它工作。 这是我的保存格式,这些是输入字段中的值

               $.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 );
            }); 
在这里,它被保存到文件:

$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);
现在我想分别回应这些值:

$string = file_get_contents("results.json");
$jsonObject = json_decode($string);
$jsonArray = json_decode($string, true); 
echo $jsonObject->Produkt->Produktkategorie . " and " . `$jsonArray['Produkt']['MaxBreite'];`
但这只会给我带来以下错误:

对于对象:注意:尝试在中获取非对象的属性 对于数组:注意:未定义索引:Produkt in

这是完整的json文件:

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

你能帮我吗?

只需将最后一行替换为

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

发布数据时,可能需要设置数据类型。
数据类型:“json”

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

        },
        data: {data:jsondata}
    });  
在php文件中,可以获得如下所示的json数据

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

希望它对您有所帮助。

您需要对json进行两次解码,因为您在文件中的方式不同。试试这个:

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

$json = json_decode($json, true);
$json = json_decode($json[0], true);
echo $json['Produkt']['Produktkategorie'];

我得到了同样的错误:注意:未定义索引:Produkt in当您尝试回显print_r($jsonObject,1)时会发生什么;打印(jsonArray,1美元);你确定
json
确实保存到文件中了吗?下面的错误:“注意:数组到字符串的转换在”是的,我每次都会检查它,我删除之前会再次保存整个文件file@Ajaypayne我还尝试了print方法,没有显示任何内容,如果我执行“print_r($jsonArray,true)”-我会在您的speichern.php中获取整个json数据,$neu=json_decode(带斜杠($_POST['data']),true);。。谢谢!:)