如何在Array中转换JSON字符串。 Array中的数组让我感到困惑。怎么处理呢?

时间:2016-02-09 10:55:45

标签: php json web-services json-deserialization

我有一个JSON字符串。我希望它填充数组。这是我收到的数组。

我的JSON回复是:

{
    "model": "SyncData",
    "unique_id": "c12fb356f90d032b",
    "key": "sdjvnsdivbsnd",
    "sync_data": {
        "array_a": [{
            "a_fav": "true",
            "a_id": 1
        }, {
            "a_fav": "false",
            "a_id": 2
        }],
        "array_b": [{
            "b_fav": "false",
            "b_id": 8
        }],
        "c_array": [{
            "c_fav": "false",
            "c_id": 15996
        }],
        "patient_list_array": [{
            "unique_id": "sdvsdvsdvdsdv",
            "p_status": "false",
            "p_id": 1454943805215,
            "p_note": "2",
            "p_code": "8",
            "p_timestamp": 1454943805216,
            "p_name": "ABC XYZ",
            "p_status": 1,
            "p_room_no": "5"
        }],

        "array_d": [{
            "d_assigned_id": "30",
            "d_fav": "true"
        }]
    }
}

我想将所有这些数据存储在Array中,并存储在DataBase中。

2 个答案:

答案 0 :(得分:2)

用于可视化JSON字符串的简单代码

<?php
$string = '{
    "model": "SyncData",
    "unique_id": "c12fb356f90d032b",
    "key": "sdjvnsdivbsnd",
    "sync_data": {
        "array_a": [{
            "a_fav": "true",
            "a_id": 1
        }, {
            "a_fav": "false",
            "a_id": 2
        }],
        "array_b": [{
            "b_fav": "false",
            "b_id": 8
        }],
        "c_array": [{
            "c_fav": "false",
            "c_id": 15996
        }],
        "patient_list_array": [{
            "unique_id": "sdvsdvsdvdsdv",
            "p_status": "false",
            "p_id": 1454943805215,
            "p_note": "2",
            "p_code": "8",
            "p_timestamp": 1454943805216,
            "p_name": "ABC XYZ",
            "p_status": 1,
            "p_room_no": "5"
        }],

        "array_d": [{
            "d_assigned_id": "30",
            "d_fav": "true"
        }]
    }
}';
$obj = json_decode($string);
print_r($obj);

将输出:

stdClass Object
(
    [model] => SyncData
    [unique_id] => c12fb356f90d032b
    [key] => sdjvnsdivbsnd
    [sync_data] => stdClass Object
        (
            [array_a] => Array
                (
                    [0] => stdClass Object
                        (
                            [a_fav] => true
                            [a_id] => 1
                        )

                    [1] => stdClass Object
                        (
                            [a_fav] => false
                            [a_id] => 2
                        )

                )

            [array_b] => Array
                (
                    [0] => stdClass Object
                        (
                            [b_fav] => false
                            [b_id] => 8
                        )

                )

            [c_array] => Array
                (
                    [0] => stdClass Object
                        (
                            [c_fav] => false
                            [c_id] => 15996
                        )

                )

            [patient_list_array] => Array
                (
                    [0] => stdClass Object
                        (
                            [unique_id] => sdvsdvsdvdsdv
                            [p_status] => 1
                            [p_id] => 1454943805215
                            [p_note] => 2
                            [p_code] => 8
                            [p_timestamp] => 1454943805216
                            [p_name] => ABC XYZ
                            [p_room_no] => 5
                        )

                )

            [array_d] => Array
                (
                    [0] => stdClass Object
                        (
                            [d_assigned_id] => 30
                            [d_fav] => true
                        )

                )

        )

)

答案 1 :(得分:1)

如果你想在数组中使用这个json字符串而不是使用

json_decode($string,true);

请注意,如果您需要以对象形式获得结果,则json_decode的第二个参数将返回数组,而不是删除第二个参数“true”。

您的代码:

$string = '{
    "model": "SyncData",
    "unique_id": "c12fb356f90d032b",
    "key": "sdjvnsdivbsnd",
    "sync_data": {
        "array_a": [{
            "a_fav": "true",
            "a_id": 1
        }, {
            "a_fav": "false",
            "a_id": 2
        }],
        "array_b": [{
            "b_fav": "false",
            "b_id": 8
        }],
        "c_array": [{
            "c_fav": "false",
            "c_id": 15996
        }],
        "patient_list_array": [{
            "unique_id": "sdvsdvsdvdsdv",
            "p_status": "false",
            "p_id": 1454943805215,
            "p_note": "2",
            "p_code": "8",
            "p_timestamp": 1454943805216,
            "p_name": "ABC XYZ",
            "p_status": 1,
            "p_room_no": "5"
        }],

        "array_d": [{
            "d_assigned_id": "30",
            "d_fav": "true"
        }]
    }
}';

$array = json_decode($string,true);
echo "<pre>";
print_r($array);

<强>结果:

Array
(
    [model] => SyncData
    [unique_id] => c12fb356f90d032b
    [key] => sdjvnsdivbsnd
    [sync_data] => Array
        (
            [array_a] => Array
                (
                    [0] => Array
                        (
                            [a_fav] => true
                            [a_id] => 1
                        )

                    [1] => Array
                        (
                            [a_fav] => false
                            [a_id] => 2
                        )

                )

            [array_b] => Array
                (
                    [0] => Array
                        (
                            [b_fav] => false
                            [b_id] => 8
                        )

                )

            [c_array] => Array
                (
                    [0] => Array
                        (
                            [c_fav] => false
                            [c_id] => 15996
                        )

                )

            [patient_list_array] => Array
                (
                    [0] => Array
                        (
                            [unique_id] => sdvsdvsdvdsdv
                            [p_status] => 1
                            [p_id] => 1454943805215
                            [p_note] => 2
                            [p_code] => 8
                            [p_timestamp] => 1454943805216
                            [p_name] => ABC XYZ
                            [p_room_no] => 5
                        )

                )

            [array_d] => Array
                (
                    [0] => Array
                        (
                            [d_assigned_id] => 30
                            [d_fav] => true
                        )

                )

        )

)