我想從json獲取具體的值,但我無法讓它工作。 這是我的保存格式,這些是來自輸入字段的值。PHP 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);
,現在我想seperately呼應這些值:
$string = file_get_contents("results.json");
$jsonObject = json_decode($string);
$jsonArray = json_decode($string, true);
echo $jsonObject->Produkt->Produktkategorie . " and " . `$jsonArray['Produkt']['MaxBreite'];`
但這只是拋出了我以下錯誤:
對象:注意:試圖獲取數組中的非對象屬性 :注意:未定義指數:PRODUKT在
這是完整的JSON文件:
["{\"Produkt\":{\"Produktkategorie\":\"TestArtikel\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Ecke\"},\"Formen\":{\"FormRund\":\"true\",\"FormEllipse\":\"true\",\"FormRechtQuad\":\"true\"}}}"]
你能幫助我嗎?
當你嘗試回顯時會發生什麼 print_r($ jsonObject,1); print_r($ jsonArray,1); – Ajaypayne
你確定'json'實際上是保存到文件中嗎? – Ionut
以下錯誤:「注意:數組到字符串轉換」 – chim