无法使用PHP解码json

时间:2015-01-04 20:32:58

标签: php json

我正在尝试解析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"
}

我需要能够在uri

时取代rel = hosted_payment

我写了以下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

我无法理解为什么会发生这种情况,请指出我犯错误的地方。

1 个答案:

答案 0 :(得分:3)

您没有使用$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更改为$links,然后将foreach中的$link变量更改为$links