简体   繁体   English

从JSON获取键/值

[英]Getting key/value from JSON

We are working with an API that brings back JSON in this format: 我们正在使用以这种格式返回JSON的API:

[
    {
        "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. 我目前正在使用foreach将其组织成一个新的,更清晰的数组 - 但代码库变得相当大。

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 首先使用json_decode转换为数组,然后您可以以普通多维数组的任何方式访问它...例如

$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 来自http://php.net/manual/en/language.types.array.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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