【问题标题】:Read var_export output of JSON response读取 JSON 响应的 var_export 输出
【发布时间】:2015-12-03 10:34:20
【问题描述】:

我得到一个 API 调用响应,它通过var_export 产生此输出:

  stdClass::__set_state(array(
      'message-count' => '1',
      'messages' =>
      array (
          0 =>
          stdClass::__set_state(array(
              'to' => '23408234',
              'message-id' => '023425F8',
              'status' => '0',
              'remaining-balance' => '152.26940000',
              'message-price' => '0.0530',
              'network' => '7432',
          )),
      ),
  ))

如何访问此内容?

我试过这些都无济于事:

  • $response->message-count
  • $response['message-count']
  • $response->messages[0]->to

【问题讨论】:

  • 回声$response->{"message-count"}
  • 你是对的,@Rizier123!谢谢!我怎样才能让你的评论成为答案? ;-)

标签: php json stdclass


【解决方案1】:

问题是,您的属性中包含相同的字符(破折号:-),不允许使用 variable name

[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

所以要仍然能够访问它,您必须使用complex curly syntax

$object->{"property-name"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 2010-10-30
    • 2016-07-18
    • 1970-01-01
    • 2020-09-13
    • 2013-09-09
    相关资源
    最近更新 更多