简体   繁体   English

使用PHP获取JSON值

[英]Grab JSON value using PHP

Below is the result of the code written below, where I would like to grab the first id element, and equal that to a php variable such as $customerID = whats in the green 下面是下面编写的代码的结果,我想抓住第一个id元素,并将其等于php变量,例如$ customerID =绿色中的whats 在此输入图像描述

Below is how this json data is generated: 以下是如何生成此json数据:

try {
 $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $amount,
      'currency' => 'cad',
            'capture' => 'false',

      'description'=>  $courseTitle

  ));

 $charge_json =  $charge->__toJSON();
 $charge_json->_values['id'];

echo "<pre>";
var_dump($charge_json);
echo "</pre>";

} catch(\Stripe\Error\Card $e) {
  // The card has been declined
  $stripChargeValid = false;
  echo "failed";

}

Note: 注意:

The notice of error found on line 58 is the below line: 第58行发现的错误通知如下:

 $charge_json->_values['id'];

The reason I did the following line was because at its core the json data being populated was protected and hence i would not be able to grab the id: 我做以下行的原因是因为它的核心是填充的json数据受到保护,因此我无法获取id:

 $charge_json =  $charge->__toJSON();

You need to json_decode() the $customer data (the json image you posted). 您需要json_decode() $ customer数据(您发布的json图像)。 Afterwards you can access the id by $customer->id . 之后您可以通过$customer->id访问$customer->id Call print_r() on the json_decoded $customer to see its structure. 在json_decoded $ customer上调用print_r()以查看其结构。

You will want to use json_decode() . 您将需要使用json_decode()

I've made you an example. 我已经为你树立了榜样。 PHPaste Snippet . PHPaste片段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM