簡體   English   中英

如何在php中將json_encoded數據轉換為關聯數組?

[英]how to convert a json_encoded data to an associative array in php?

我有這個數據。如何將其轉換為php關聯數組呢?實際上我很困惑。我嘗試了json_decode並反序列化 。這里有人可以幫助我。

  "data": {
    "address": "101, B Wing, Ashapura Park, Dombivli East",
    "area": "thane",
    "area_id": 51466,

    "field_agent": {
      "email": "abin@example.in",
      "name": "Jacob Mattom",
      "phone": 2147483647
    },
    "geo_location_city": "thane",
    "geo_location_city_id": 2430,
    "keywords": [
      "sample for tesing"
    ],
    "lat": null,
    "lng": null,
    "location_id": 5578,
    "media": [
      {
        "MediaId": "0bd41ec2f0bc11e58fd5066a28c33ece",
        "thumb_url": "http://example-datapipeline-staging.s3-ap-southeast-1.examplezz.com/0bd41ec2f0bc11e58fd5066a28c33ece.mp4",
      },
      {
        "MediaId": "28c3823ef0bc11e5b825066a28c33ece",
        "thumb_url": "http://example-datapipeline-staging.s3-ap-southeast-1.examplezz.com/28c3823ef0bc11e5b825066a28c33ece.mp4",
      }
    ],
    "meta": {
      "Product": [
        {
          "brand": "",
          "categorykeywords": [
            "Construction Tools Indl",
            "Construction Tools",
            "Industrial Construction Tools",
            "Tools",
            ""
          ],
          "categorykeywordsexact": [
            "Construction Tools Indl",
            "Construction Tools",
            "Industrial Construction Tools",
            "Tools",
            ""
          ],
          "categorypath": "Top/Construction Equipments/Construction Tools- Indl",
          "description": "",
          "id": 10395376,
          "imageurls": "",
          "intattribute": [
            {
              "answer": 0,
              "qaggr": "",
              "question": ""
            },
            {
              "answer": 0,
              "qaggr": "",
              "question": ""
            },
            {
              "answer": 0,
              "qaggr": "",
              "question": ""
            },
            {
              "answer": 0,
              "qaggr": "",
              "question": ""
            }
          ],
          "keywords": [],
          "l1category": "Building & Construction",
          "l2category": "Construction Equipments",
          "name": "",
          "parkedkeywords": [],
          "stringattribute": [
            {
              "aaggr": [
                "New"
              ],
              "answer": [
                "New"
              ],
              "qaggr": "Condition",
              "question": "Condition"
            },
            {
              "aaggr": [
                "Customized",
                "Warranty"
              ],
              "answer": [
                "Customized",
                "Warranty"
              ],
              "qaggr": "Services",
              "question": "Services"
            },
            {
              "aaggr": [
                "Electric",
                "Hydraulic",
                "Pneumatic"
              ],
              "answer": [
                "Electric",
                "Hydraulic",
                "Pneumatic"
              ],
              "qaggr": "Technology",
              "question": "Technology"
            },
            {
              "aaggr": [
                "Cutting Tools",
                "Hand Tools",
                "Plumbing Tools",
                "Power Tools"
              ],
              "answer": [
                "Cutting Tools",
                "Hand Tools",
                "Plumbing Tools",
                "Power Tools"
              ],
              "qaggr": "Tools",
              "question": "Tools"
            }
          ]
        }
      ],
      "keywords1": [
        "sample for tesing"
      ],
      "project": "ace"
    },
    "outlet_status": "Valid Data",
    "outlet_type": 0,
    "phones": [
      {
        "type": "mobile",
        "value": "+9198955548554"
      },
      {
        "type": "tollfree",
        "value": ""
      }
    ],
    "pincode": "22601",
    "pincode_id": 151522,
    "products": null,
    "progress": "field executive assigned",
    "qc_agency": null,
    "qc_agent": null
  },
  "message": null,
  "status": true
}

我有這個數據。如何將其轉換為php關聯數組呢?實際上我很困惑。我嘗試了json_decode並反序列化 。這里有人可以幫助我。

我相信最簡單的解決方案是使用json_decode。

$json= '"data": {"address": "101, B Wing, Ashapura Park, Dombivli East", "area": "thane", "area_id": 51466, "field_agent": {"email": "abin@example.in", "name": "Jacob Mattom", "phone": 2147483647 }}';

$obj = json_decode($json);

echo $obj->address;
echo $obj->field_agent->email;

您的JSON數據結構錯誤。 因此,PHP的json_decode無法使用。 首先修復它。 要解決此問題,您可以使用在線json編輯器。 您的json數據出了什么問題:

  1. 在大括號之間寫入數據
  2. 列表的最后一項不要使用逗號

現在可以使用json_decode了。

這是JSON數據的固定版本: http : //www.jsoneditoronline.org/?id=689de1b78324faaced8018a2ccda1845

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM