如何合并JSON两个索引然后解析信息

时间:2014-07-25 07:57:12

标签: php jquery json

我正在尝试迭代 JSON (玩家和建筑物)的两个索引,以便我可以在 jQuery

中获得新结果

我有两个 JSON 索引,其中一个有玩家信息,第二个索引有建筑相关玩家信息。

我想解析它,以便我可以获得播放器及其建筑名称。

我的实际JSON结果

{
        "Players": [
            {
                "id": "35",
                "building_id": "8",
                "room_num": "101",
            },
            {
                "id": "36",
                "building_id": "9",
                "room_num": "102",
            },
            {
                "id": "37",
                "building_id": "10",
                "room_num": "103",
            },
            {
                "id": "38",
                "building_id": "11",
                "room_num": "104",
            }
        ],
        "Buildings": [
            {
                "id": "8",
                "name": "ABC"
            },
            {
                "id": "9",
                "name": "DEF"
            },
            {
                "id": "10",
                "name": "GHI"
            },
            {
                "id": "11",
                "name": "JKL"
            }
        ]
    }

需要此

 "information": [
                {
                    "player_id": "35",
                    "Buildings_name": "ABC"
                },
                {
                    "player_id": "36",
                    "Buildings_name": "DEF"
                },
                {
                    "player_id": "37",
                    "Buildings_name": "GHI"
                },
                {
                    "player_id": "38",
                    "Buildings_name": "JKL"
                }
            ]
        }

2 个答案:

答案 0 :(得分:3)

在这里,您可以选择每个玩家并检查是否有建筑物,并将它们映射到新结构。这不会过滤没有映射到玩家的建筑物的值,也不会包含没有玩家的建筑物。

var x = {
    "Players": [
        {
            "id": "35",
            "building_id": "8",
            "room_num": "101",
        },
        {
            "id": "36",
            "building_id": "9",
            "room_num": "102",
        },
        {
            "id": "37",
            "building_id": "10",
            "room_num": "103",
        },
        {
            "id": "38",
            "building_id": "11",
            "room_num": "104",
        }
    ],
    "Buildings": [
        {
            "id": "8",
            "name": "ABC"
        },
        {
            "id": "9",
            "name": "DEF"
        },
        {
            "id": "10",
            "name": "GHI"
        },
        {
            "id": "11",
            "name": "JKL"
        }
    ]
};

var res = $.map(x.Players, function(item) {
    return {
        player_id: item.id,
        building_name: $.grep(x.Buildings, function(i) {
            return i.id == item.building_id
        }).length != 0 ? $.grep(x.Buildings, function(i) {
            return i.id == item.building_id
        })[0].name : undefined
    }
})

如果您想过滤没有关系的值,例如INNER join

var resInnerJoin = $.grep($.map(x.Players, function(item) {
    return {
        player_id: item.id,
        building_name: $.grep(x.Buildings, function(i) {
            return i.id == item.building_id
        }).length != 0 ? $.grep(x.Buildings, function(i) {
            return i.id == item.building_id
        })[0].name : undefined
    }
}), function(it) {
    return it.building_name != undefined
})

答案 1 :(得分:1)

如果您需要PHP:

$json = '{...}';

// create and PHP array with you json data.
$array = json_decode($json, true);

// make an array with buildings informations and with building id as key
$buildings = array();
foreach( $array['Buildings'] as $b ) $buildings[$b['id']] = $b;


$informations = array();

for ( $i = 0 ; $i < count($array['Players']) ; $i++ )
{
    $informations[$i] = array(
        'player_id' => $array['Players'][$i]['id'],
        'Buildings_name' => $buildings[$array['Players'][$i]['building_id']]['name']
    );
}

$informations = json_encode($informations);

var_dump($informations);