我试图从php中的以下JSON代码中提取销售ID。我想要获得的输出是:
1KE4800207592173L
JSON代码。
{
"transactions": [
{
"description": "This is the payment transaction description.",
"related_resources": [
{
"sale":
{
"id": "1KE4800207592173L",
"create_time": "2013-01-30T23:44:26Z",
"update_time": "2013-01-30T23:44:28Z",
"state": "completed",
"parent_payment": "PAY-34629814WL663112AKEE3AWQ",
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/1KE4800207592173L",
"rel": "self",
"method": "GET"
}]
}
}]
}],
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY- 34629814WL663112AKEE3AWQ",
"rel": "self",
"method": "GET"
}]
}
任何帮助尝试将其作为PHP JSON或数组引用都将非常感激。感谢。
编辑:
我尝试过以下方法:
$response1 = json_decode($response);
$payment_id = $response1->transactions->related_resources->sale->id;
$payment_id = print_r($payment_id, true);
和
$response1 = json_decode($response);
$payment_id = $response1->transactions->related_resources[0]->sale->id;
$payment_id = print_r($payment_id, true);
似乎都不起作用。
原始JSON与此处可找到的相同。 https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/advanced-payments-api/execute-payments/如果有帮助的话。
答案 0 :(得分:1)
试试这个
使用 json_decode 将json字符串转换为对象
$a = '{
"transactions": [
{
"description": "This is the payment transaction description.",
"related_resources": [
{
"sale":
{
"id": "1KE4800207592173L",
"create_time": "2013-01-30T23:44:26Z",
"update_time": "2013-01-30T23:44:28Z",
"state": "completed",
"parent_payment": "PAY-34629814WL663112AKEE3AWQ",
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/1KE4800207592173L",
"rel": "self",
"method": "GET"
}]
}
}]
}],
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY- 34629814WL663112AKEE3AWQ",
"rel": "self",
"method": "GET"
}]
}';
print_r(json_decode($a)->transactions[0]->related_resources[0]->sale->id);
答案 1 :(得分:0)
使用
$array = json_decode($jsonScriptVariable, true);
它将转换为数组,然后您可以轻松访问。
<?php
$var = '{
"transactions": [
{
"description": "This is the payment transaction description.",
"related_resources": [
{
"sale":
{
"id": "1KE4800207592173L",
"create_time": "2013-01-30T23:44:26Z",
"update_time": "2013-01-30T23:44:28Z",
"state": "completed",
"parent_payment": "PAY-34629814WL663112AKEE3AWQ",
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/sale/1KE4800207592173L",
"rel": "self",
"method": "GET"
}]
}
}]
}],
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY- 34629814WL663112AKEE3AWQ",
"rel": "self",
"method": "GET"
}]
}';
$array = json_decode($var, true);
echo $array['transactions'][0]['related_resources'][0]['sale']['id'];
你可以使用简单的 json_decode($ var); 来提供对象,你可以访问
echo $array->transactions[0]->related_resources[0]->sale->id;