Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

json - Access a PHP-object with dollar-sign as node name

I'm working with the YouTube Data API per PHP and requesting a video feed from a specific user in json-format. The result after json_decode is the following (shortened example):

stdClass Object
(
    [version] => 1.0
    [encoding] => UTF-8
    [feed] => stdClass Object
        (
            [xmlns] => http://www.w3.org/2005/Atom
            [xmlns$media] => http://search.yahoo.com/mrss/
            [xmlns$openSearch] => http://a9.com/-/spec/opensearchrss/1.0/
            [xmlns$gd] => http://schemas.google.com/g/2005
            [xmlns$yt] => http://gdata.youtube.com/schemas/2007
    )
)

My question is: how can I access for example the node "xmlns$media" with PHP? Within the dollar-sign it won't work or is there a way which I didn't get yet?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This will work:

echo $object->feed->{'xmlns$media'};

Alternatively, you can tell json_decode to return an array:

$array = json_decode($json, true);
echo $array['feed']['xmlns$media'];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...