如何访问JSON的某些部分?

时间:2019-06-04 05:00:23

标签: php json

我正在尝试在以下代码中访问文本“ Passion”,但我不知道如何操作。现在我只能得到

{"parts": [{"text": "Passion"]}

谁能告诉我下一步该怎么做?谢谢!

$test = 
'{
    "event":
    {
        "response":"{\"parts\": [{\"text\": \"Passion\"]}"
    },
    "event_source":"server"
}';

$jarray = json_decode($test, true);
echo $jarray['event']['response'];

1 个答案:

答案 0 :(得分:2)

您的json数据在这里无效:-

"{\"parts\": [{\"text\": \"Passion\"]}" 
//----------------------------------^ one } needed before ]

更正输入后,请执行以下操作:

<?php

$test = 
'{
    "event":
    {
        "response":"{\"parts\": [{\"text\": \"Passion\"}]}"
    },
    "event_source":"server"
}';

$jarray = json_decode($test, true);

print_r($jarray);

$response = json_decode($jarray['event']['response'], true);

echo $response['parts'][0]['text'];

输出:-https://3v4l.org/dmPDE