无法使用php解码json
我试图在php中解析一个json变量并提取一个特定的值 我的json是无法使用php解码json,php,json,Php,Json,我试图在php中解析一个json变量并提取一个特定的值 我的json是 { "currencyCode": "USD", "id": "278QQZYEIEJDSMA1L2", "link": [ { "rel": "hosted_payment", "uri": "https://pay.test.netbanx.com/hosted/v1/payment/53616c7465645f5fac1b7ed6a320e392498376e38afc42a
{
"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\u付款时提取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;
}
}
我无法理解为什么会发生这种情况,请指出我犯错误的地方。您没有使用$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;
}
}
您没有使用$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;
}
}
您没有使用$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;
}
}
您没有使用$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
,而不是$link
。跳过临时变量。变量$link在哪里?它没有用您正在分配给$posts
,而不是$link
。跳过临时变量。变量$link在哪里?它没有用您正在分配给$posts
,而不是$link
。跳过临时变量。变量$link在哪里?它没有用您正在分配给$posts
,而不是$link
。跳过临时变量。变量$link在哪里,它的值无效!!!我忽略了变量是$posts
谢谢,伙计:)是多么愚蠢啊!!!我忽略了变量是$posts
谢谢,伙计:)是多么愚蠢啊!!!我忽略了变量是$posts
谢谢,伙计:)是多么愚蠢啊!!!我忽略了变量是$posts
谢谢,伙计:)这是多么愚蠢啊