dtcyv3985 2012-06-21 01:42
浏览 43
已采纳

json在php中解析

I have the following JSON String

{
        "name":"Product",
        "properties":
        {
                "id":
                {
                        "type":"number",
                        "description":"Product identifier",
                        "required":true
                },
                "name":
                {
                        "type":"string",
                        "description":"Name of the product",
                        "required":true
                },
                "price":
                {
                        "type":"number",
                        "minimum":0,
                        "required":true
                },
                "tags":
                {
                        "type":"array",
                        "items":
                        {
                                "type":"string"
                        }
                },
                "stock":
                {
                        "type":"object",
                        "properties":
                        {
                                "warehouse":
                                {
                                        "type":"number"
                                },
                                "retail":
                                {
                                        "type":"number"
                                }
                        }
                }
        }
}    

I would like to access

properties - > stock - > properties - > warehouse.

In python I can do the following.

f = open("c:/dir/jsondec.json")
data = json.load(f)
node = data['properties']['stock']['properties']['warehouse']
print str(node)

I'm trying to do the same thing in PHP. I know I can use json_decode() but what should be the correct syntax. Also If I have an array within say within properties-> ID I could have done ['properties'][0]['id'] to access that. What would be the equivalent in php?

  • 写回答

3条回答 默认 最新

  • duanniesui6391 2012-06-21 01:48
    关注

    Version in Python

    This is in Python:

    f = open("c:/dir/jsondec.json")
    data = json.load(f)
    node = data['properties']['stock']['properties']['warehouse']
    print str(node)
    

    Version in PHP

    And this is its equivalent in PHP:

    $f = file_get_contents('c:/dir/jsondec.json');
    $data = json_decode($f, true);
    $node = $data['properties']['stock']['properties']['warehouse'];
    echo $node;
    

    "Gotchas" (or "slight differences")

    There is one difference however: f in Python version is opened file, while $f in PHP version is already a string.

    As RPM correctly noted, there is another difference: arrays in PHP are converted to string "Array" when used in string context (see here: http://ideone.com/XJfSP), so you probably would like to use:

    print_r($node);
    

    or

    var_dump($node);
    

    instead of

    echo $node;
    

    to see the actual content of the array.

    EDIT: Changed json_decode() result into array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?