-1

我有一些 JSON 数据如下:

{
    "TABLE": [{
        "ROW": [{
            "COL": [{
                "DATA": "Buff momo"
            }, {
                "DATA": "60.00"
            }, {
                "DATA": "1;#"
            }, {
                "DATA": "0"
            }, {
                "DATA": "1"
            }, {
                "DATA": "1"
            }, {
                "DATA": "118"
            }, {
                "DATA": "1;#{5D73B50D-2FFC-4D16-881E-D49328447AAB}"
            }, {
                "DATA": "1;#0"
            }, {
                "DATA": "2010-06-21 15:43:53"
            }, {
                "DATA": "Miscellaneous"
            }, {
                "DATA": "1;#Home/Lists/Canteen Menu/1_.000"
            }]
        }, {
            "COL": [{
                "DATA": "Chicken drumstick"
            }, {
                "DATA": "100.00"
            }, {
                "DATA": "3;#"
            }, {
                "DATA": "0"
            }, {
                "DATA": "1"
            }, {
                "DATA": "3"
            }, {
                "DATA": "40"
            }, {
                "DATA": "3;#{A1E0F087-57D3-4039-991B-E08B1CB3892A}"
            }, {
                "DATA": "3;#0"
            }, {
                "DATA": "2010-06-21 15:44:14"
            }, {
                "DATA": "Chinese"
            }, {
                "DATA": "3;#Home/Lists/Canteen Menu/3_.000"
            }]
        }
        }]
    }]
}]
}
""

我尝试将其解码如下:

header('Content-Type: application/json');
$json_string = utf8_encode(file_get_contents("filename"));
$parsed_json = json_decode($json_string, true);
foreach ($parsed_json as $key => $value) {
}

但无济于事。我是 gettig 错误,因为为 foreach() 提供的参数无效。我在哪里做错了?

4

2 回答 2

1

您的 JSON 无效。有些括号不属于那里。正确的 JSON 是

{ "TABLE":[{ "ROW":[ { "COL":[ {"DATA":"Buff momo"},{"DATA":"60.00"},{"DATA":"1;#"},{"DATA":"0"},{"DATA":"1"},{"DATA":"1"},{"DATA":"118"},{"DATA":"1;#{5D73B50D-2FFC-4D16-881E-D49328447AAB}"},{"DATA":"1;#0"},{"DATA":"2010-06-21 15:43:53"},{"DATA":"Miscellaneous"},{"DATA":"1;#Home/Lists/Canteen Menu/1_.000"}]}, { "COL":[ {"DATA":"Chicken drumstick"},{"DATA":"100.00"},{"DATA":"3;#"},{"DATA":"0"},{"DATA":"1"},{"DATA":"3"},{"DATA":"40"},{"DATA":"3;#{A1E0F087-57D3-4039-991B-E08B1CB3892A}"},{"DATA":"3;#0"},{"DATA":"2010-06-21 15:44:14"},{"DATA":"Chinese"},{"DATA":"3;#Home/Lists/Canteen Menu/3_.000"}]}]} ]}

您可以在http://json.parser.online.fr/测试您的 JSON

于 2013-07-28T10:58:21.813 回答
0

使用json_decode函数解码json

*更新:将每个 COL 显示为列表的示例

<?php
$json_string = '{ "TABLE":[{ "ROW":[ { "COL":[ {"DATA":"Buff momo"},{"DATA":"60.00"},{"DATA":"1;#"},{"DATA":"0"},{"DATA":"1"},{"DATA":"1"},{"DATA":"118"},{"DATA":"1;#{5D73B50D-2FFC-4D16-881E-D49328447AAB}"},{"DATA":"1;#0"},{"DATA":"2010-06-21 15:43:53"},{"DATA":"Miscellaneous"},{"DATA":"1;#Home/Lists/Canteen Menu/1_.000"}]}, { "COL":[ {"DATA":"Chicken drumstick"},{"DATA":"100.00"},{"DATA":"3;#"},{"DATA":"0"},{"DATA":"1"},{"DATA":"3"},{"DATA":"40"},{"DATA":"3;#{A1E0F087-57D3-4039-991B-E08B1CB3892A}"},{"DATA":"3;#0"},{"DATA":"2010-06-21 15:44:14"},{"DATA":"Chinese"},{"DATA":"3;#Home/Lists/Canteen Menu/3_.000"}]}]} ]}';
$parsed_json = json_decode($json_string);
foreach ($parsed_json->TABLE[0]->ROW as $key => $value) {
    echo 'SELECT ' . $key . ': <select name="select' . $key . '">';
    foreach ($value->COL as $col) {
        echo '<option value="' . $col->DATA . '">' . $col->DATA . '</option>';
    }
    echo '</select>';
}
?>
于 2013-07-28T10:52:24.230 回答