如何在php中将jsonencoded数据转换为关联数组


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

我有这个数据。如何将它转换为php关联数组?其实我很困惑。我试过使用json_decodeunserialize。这里的任何人都可以帮助我。提前感谢。

  "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": [
            {
              "answerswer": 0,
              "qaggr": "",
              "question": ""
            },
            {
              "answerswer": 0,
              "qaggr": "",
              "question": ""
            },
            {
              "answerswer": 0,
              "qaggr": "",
              "question": ""
            },
            {
              "answerswer": 0,
              "qaggr": "",
              "question": ""
            }
          ],
          "keywords": [],
          "l1category": "Building & Construction",
          "l2category": "Construction Equipments",
          "name": "",
          "parkedkeywords": [],
          "stringattribute": [
            {
              "aaggr": [
                "New"
              ],
              "answerswer": [
                "New"
              ],
              "qaggr": "Condition",
              "question": "Condition"
            },
            {
              "aaggr": [
                "Customized",
                "Warranty"
              ],
              "answerswer": [
                "Customized",
                "Warranty"
              ],
              "qaggr": "Services",
              "question": "Services"
            },
            {
              "aaggr": [
                "Electric",
                "Hydraulic",
                "Pneumatic"
              ],
              "answerswer": [
                "Electric",
                "Hydraulic",
                "Pneumatic"
              ],
              "qaggr": "Technology",
              "question": "Technology"
            },
            {
              "aaggr": [
                "Cutting Tools",
                "Hand Tools",
                "Plumbing Tools",
                "Power Tools"
              ],
              "answerswer": [
                "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_decodeunserialize。这里的任何人都可以帮助我。提前感谢。

我认为最简单的解决方案是使用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