简体   繁体   中英

Getting key/value from JSON

We are working with an API that brings back JSON in this format:

[
    {
        "Id": "d7526186-361c-e611-80da-00155df41a0a",
        "LogicalName": "contact",
        "Attributes": [
            {
                "Key": "customertypecode",
                "Value": {
                    "Value": 1
                }
            },
            {
                "Key": "merged",
                "Value": false
            },
            {
                "Key": "territorycode",
                "Value": {
                    "Value": 1
                }
            }
        ],
        "EntityState": null,
        "FormattedValues": [
            {
                "Key": "customertypecode",
                "Value": "Default Value"
            },
            {
                "Key": "address2_addresstypecode",
                "Value": "Default Value"
            },
            {
                "Key": "merged",
                "Value": "No"
            },
            {

I am currently using foreach to organise this into a new, cleaner array - but the code base is getting rather large.

What would be the cleanest way of getting specific values based on specifying a key name?

Thanks so much for your help.

First using json_decode to convert to an array, then you can access it in any way you would a normal multi dimensional array... eg

$array = array(
    "foo" => "bar",
    42    => 24,
    "multi" => array(
         "dimensional" => array(
             "array" => "foo"
         )
    )
);

var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);

from http://php.net/manual/en/language.types.array.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM