Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用php解码json_Php_Json - Fatal编程技术网

无法使用php解码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

我试图在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\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
谢谢,伙计:)这是多么愚蠢啊