简体   繁体   中英

How to access certain part of a JSON?

I'm trying to access the text "Passion" in the following code, but I can't figure out how. Right now I can only get

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

Can anyone tell me what to do next? Thanks!

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

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

you json data is invalid here:-

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

After correcting the input do like below:

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

Output:- https://3v4l.org/dmPDE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM