[英]unable to decode json using php
我正在尝试解析php中的json变量并增加一个特定值。
我的json是
{
"currencyCode": "USD",
"id": "278QQZYEIEJDSMA1L2",
"link": [
{
"rel": "hosted_payment",
"uri": "https://pay.test.netbanx.com/hosted/v1/payment/53616c7465645f5fac1b7ed6a320e392498376e38afc42ada7b2c646af7de3e4bc58c77905e8032d"
},
{
"rel": "self",
"uri": "https://devcentre4619:B-qa2-0-548a0405-302c02140e37a359f831dab53896f0cd7ba5d312787b7070021416ab2196d33848a5e936f65ff50f780d8a08aeb6@api.test.netbanx.com/hosted/v1/orders/278QQZYEIEJDSMA1L2"
},
{
"rel": "resend_callback",
"uri": "https://devcentre4619:B-qa2-0-548a0405-302c02140e37a359f831dab53896f0cd7ba5d312787b7070021416ab2196d33848a5e936f65ff50f780d8a08aeb6@api.test.netbanx.com/hosted/v1/orders/278QQZYEIEJDSMA1L2/resend_callback"
}
],
"merchantRefNum": "89983481",
"mode": "live",
"totalAmount": "1234",
"type": "order"
}
当rel = hosted_payment
时,我需要能够提取uri
我写了下面的PHP代码
<?php
$jsonData = '{
"currencyCode": "USD",
"id": "278QQZYEIEJDSMA1L2",
"link": [
{
"rel": "hosted_payment",
"uri": "https://pay.test.netbanx.com/hosted/v1/payment/53616c7465645f5fac1b7ed6a320e392498376e38afc42ada7b2c646af7de3e4bc58c77905e8032d"
},
{
"rel": "self",
"uri": "https://devcentre4619:B-qa2-0-548a0405-302c02140e37a359f831dab53896f0cd7ba5d312787b7070021416ab2196d33848a5e936f65ff50f780d8a08aeb6@api.test.netbanx.com/hosted/v1/orders/278QQZYEIEJDSMA1L2"
},
{
"rel": "resend_callback",
"uri": "https://devcentre4619:B-qa2-0-548a0405-302c02140e37a359f831dab53896f0cd7ba5d312787b7070021416ab2196d33848a5e936f65ff50f780d8a08aeb6@api.test.netbanx.com/hosted/v1/orders/278QQZYEIEJDSMA1L2/resend_callback"
}
],
"merchantRefNum": "89983481",
"mode": "live",
"totalAmount": "1234",
"type": "order"
}';
// convert the string to a json object
$jfo = json_decode($jsonData);
// copy the link array to a php var
$posts = $jfo->link;
// listing links
foreach ($link as $link) {
if (strcmp("hosted_payment", $link->rel) == 0) {
echo $link->uri;
}
}
但是我得到了错误
Notice: Undefined variable: link in E:\xampp\htdocs\json\decode.php on line 31
Warning: Invalid argument supplied for foreach() in E:\xampp\htdocs\json\decode.php on line 31
我无法理解为什么会这样,请指出我在哪里出错。
您没有使用$link
变量。
<?php
// setting json data
$jsonData = '{
"currencyCode": "USD",
"id": "278QQZYEIEJDSMA1L2",
"link": [
{
"rel": "hosted_payment",
"uri": "https://pay.test.netbanx.com/hosted/v1/payment/53616c7465645f5fac1b7ed6a320e392498376e38afc42ada7b2c646af7de3e4bc58c77905e8032d"
},
{
"rel": "self",
"uri": "https://devcentre4619:B-qa2-0-548a0405-302c02140e37a359f831dab53896f0cd7ba5d312787b7070021416ab2196d33848a5e936f65ff50f780d8a08aeb6@api.test.netbanx.com/hosted/v1/orders/278QQZYEIEJDSMA1L2"
},
{
"rel": "resend_callback",
"uri": "https://devcentre4619:B-qa2-0-548a0405-302c02140e37a359f831dab53896f0cd7ba5d312787b7070021416ab2196d33848a5e936f65ff50f780d8a08aeb6@api.test.netbanx.com/hosted/v1/orders/278QQZYEIEJDSMA1L2/resend_callback"
}
],
"merchantRefNum": "89983481",
"mode": "live",
"totalAmount": "1234",
"type": "order"
}';
// convert the string to a json object
$jfo = json_decode($jsonData);
// copy the link array to a php var
$links = $jfo->link;
// listing links
foreach ($links as $link) {
if (strcmp("hosted_payment", $link->rel) == 0) {
echo $link->uri;
}
}
只需将$posts
更改$posts
$links
,并将foreach中的$link
变量更改为$links
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.