PHP json_decode notation issue

前端 未结 2 1209
无人及你
无人及你 2021-01-27 10:07

I am having an issue dealing with the notation used in a JSON file I am trying parse. Some of the nodes have . (periods) in the names which escapes object-notation ($json

相关标签:
2条回答
  • 2021-01-27 10:32

    You can use braces around the name to access the property:

    <?php
    
    $o = json_decode('{"docs": [{"rssFeed.type": "news", "rssFeed.url": "http://www.example.com/",  "score": 1.0 }]}');
    
    var_dump($o->docs[0]->{'rssFeed.url'});
    ?>
    
    0 讨论(0)
  • 2021-01-27 10:42

    @eWolf: you are still right, php is almost like javascript as its C like nature.

    $object = json_decode('{"docs": [{"rssFeed.type": "news", 
                                      "rssFeed.url": "http://www.example.com/",
                                      "score": 1.0 }]}', TRUE);
    

    Note: the 2nd argument to json_decode(), TRUE. It forces the function to return an associative array.

    Now $object['rssFeed.type'] can be used.

    0 讨论(0)
提交回复
热议问题