在PHP中处理JSON数据

时间:2016-11-01 08:20:52

标签: php json

我在php脚本中得到了一个json数据,这个json数据存储在我的变量中。现在我想查看"价格"名字"表达"在PHP中。请指导我在php中提取数据的方法。

  

$ serviceTypesJSON = json_decode($ rawBody,true);

{
  "services": {
    "service": [
      {
        "code": "INT_PARCEL_COR_OWN_PACKAGING",
        "name": "Courier",
        "price": "85.13",
        "max_extra_cover": 5000,
        "options": {
          "option": [
            {
              "code": "INT_TRACKING",
              "name": "Tracking"
            },
            {
              "code": "INT_SMS_TRACK_ADVICE",
              "name": "SMS track advice"
            },
            {
              "code": "INT_EXTRA_COVER",
              "name": "Extra Cover"
            }
          ]
        }
      },
      {
        "code": "INT_PARCEL_EXP_OWN_PACKAGING",
        "name": "Express",
        "price": "40.13",
        "max_extra_cover": 5000,
        "options": {
          "option": [
            {
              "code": "INT_TRACKING",
              "name": "Tracking"
            },
            {
              "code": "INT_SIGNATURE_ON_DELIVERY",
              "name": "Signature on delivery"
            },
            {
              "code": "INT_SMS_TRACK_ADVICE",
              "name": "SMS track advice"
            },
            {
              "code": "INT_EXTRA_COVER",
              "name": "Extra Cover"
            }
          ]
        }
      },
      {
        "code": "INT_PARCEL_STD_OWN_PACKAGING",
        "name": "Standard",
        "price": "31.40",
        "max_extra_cover": 5000,
        "options": {
          "option": [
            {
              "code": "INT_TRACKING",
              "name": "Tracking"
            },
            {
              "code": "INT_EXTRA_COVER",
              "name": "Extra Cover"
            },
            {
              "code": "INT_SIGNATURE_ON_DELIVERY",
              "name": "Signature on delivery"
            },
            {
              "code": "INT_SMS_TRACK_ADVICE",
              "name": "SMS track advice"
            }
          ]
        }
      },
      {
        "code": "INT_PARCEL_AIR_OWN_PACKAGING",
        "name": "Economy Air Parcels",
        "price": "23.77",
        "max_extra_cover": 500,
        "options": {
          "option": [
            {
              "code": "INT_EXTRA_COVER",
              "name": "Extra Cover"
            },
            {
              "code": "INT_SIGNATURE_ON_DELIVERY",
              "name": "Signature on delivery"
            }
          ]
        }
      }
    ]
  }
}

现在如何从这个变量中提取"名称":" Express"," price":" 40.13"?我想采取快递,标准,经济航空包裹的价格。请帮我如何从这个json混合数据中提取确切的数据

1 个答案:

答案 0 :(得分:0)

var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}';

var obj = JSON.parse(text);

document.getElementById("demo").innerHTML =
obj.name + "<br>" +
obj.street + "<br>" +
obj.phone;

您可以使用此方法。