dongzhuang5741 2019-06-14 11:53
浏览 87

由于JSON中的奇怪OBJECT名称,难以以PHP数组的形式获取JSON数据

I am fetching data from JSON in the form of PHP array. But I am not able to fetch it due to strange OBJECT titles.

E.g. here is simply example

[car] => stdClass Object
    (
        [model] => stdClass Object
            (
                [year] => 2018
                [company] => Honda
                [condition] => Good
            )

    )

Now I can fetch "condition" like this $car->model->condition;

But in my case, the JSON is like this

[car] => stdClass Object
    (
        [field_set_key="profile",username="sammy"] => stdClass Object
            (
                [year] => 2018
                [company] => Honda
                [condition] => Good
            )

    )

I am not able to fetch "condition" value due to this strange object [field_set_key="profile",username="sammy"]

What should I do?

$car->[ ?????? ]->condition;
  • 写回答

1条回答 默认 最新

  • duanhuan8983 2019-06-14 12:02
    关注

    Using the {} syntax you can address that like this

    echo $car->{'field_set_key="profile",username="sammy"'}->condition;
    

    Its ugly and it would really be better if you got the people producing this data to fix their code.

    Example

    $car = new stdClass;
    $b = new stdClass;
    
    $b->year = 2018;
    $b->company = 'Honda';
    $b->condition =  'good';
    
    $car->{'field_set_key="profile",username="sammy"'} = $b;
    
    print_r($car) . PHP_EOL;
    
    echo 'The condition of the '
            . $car->{'field_set_key="profile",username="sammy"'}->year . ' '
            . $car->{'field_set_key="profile",username="sammy"'}->company
            . ' is ' . $car->{'field_set_key="profile",username="sammy"'}->condition;
    

    RESULTS

    stdClass Object
    (
        [field_set_key="profile",username="sammy"] => stdClass Object
            (
                [year] => 2018
                [company] => Honda
                [condition] => good
            )
    )
    
    The condition of the 2018 Honda is good
    
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的