如何在PHP中解析此JSON
我根据在我的网站上生成的表单构建这个JSON。 现在我想将单个项推送到数据库中,所以我需要能够通过迭代拉出指令 $instruction0 $instruction1 以及说明书中的成分 当试图解析JSON时,我得到一个错误,我不知道如何在PHP中读取它。(又名PHP NOOB)我能够通过Python获得我的所有项目所需的内容,没有任何问题如何在PHP中解析此JSON,json,Json,我根据在我的网站上生成的表单构建这个JSON。 现在我想将单个项推送到数据库中,所以我需要能够通过迭代拉出指令 $instruction0 $instruction1 以及说明书中的成分 当试图解析JSON时,我得到一个错误,我不知道如何在PHP中读取它。(又名PHP NOOB)我能够通过Python获得我的所有项目所需的内容,没有任何问题 [ [ { "instruction": "Enter text here...asdada" }, {
[
[
{
"instruction": "Enter text here...asdada"
},
{
"ingredient": "Beetroot",
"amount": "2",
"type": "grams"
},
{
"ingredient": "Beetroot",
"amount": "1",
"type": "grams"
}
],
[
{
"instruction": "Enter text here..sdfsdf."
},
{
"ingredient": "Carrot",
"amount": "2",
"type": "grams"
},
{
"ingredient": "Beetroot",
"amount": "525",
"type": "grams"
}
]
]
您是否尝试过使用第二个参数“true”指定您希望输出为关联数组
json_decode($json, true);
其次,如果您要手动将此json字符串发布到代码中,则需要将其包装在引号中,因此基于此
$json = '[ [ { "instruction": "Enter text here...asdada" }, { "ingredient": "Beetroot", "amount": "2", "type": "grams" }, { "ingredient": "Beetroot", "amount": "1", "type": "grams" } ], [ { "instruction": "Enter text here..sdfsdf." }, { "ingredient": "Carrot", "amount": "2", "type": "grams" }, { "ingredient": "Beetroot", "amount": "525", "type": "grams" } ] ] ';
$array = json_decode($json, true);
然后,您可以在
$array
中进行迭代,因为正常情况下屏幕截图没有任何帮助。请粘贴您正在编写的实际代码,以及您看到的错误消息。