在PHP中将json字符串反向转换为真正的json数组

时间:2018-05-14 15:58:59

标签: php

我的数据库中存储了以下内容:

"{\"id\":10,\"name\":\"Foobar\",\"sheep\":5,\"type\":\"test\",\"created_at\":\"2017-12-20 17:51:41\",\"updated_at\":\"2017-12-20 17:51:41\",\"title\":\"Sheep\"},{\"id\":13,\"name\":\"Foobar\",\"price\":5,\"type\":\"day\",\"created_at\":\"2017-12-21 18:02:28\",\"updated_at\":\"2017-12-21 18:02:28\",\"title\":\"Hello\"},{\"id\":15,\"name\":\"Car\",\"price\":5,\"type\":\"day\",\"created_at\":\"2018-03-16 11:16:59\",\"updated_at\":\"2018-03-16 11:16:59\",\"title\":\"Car\"}"

但那不是有效的JSON字符串。如何将此字符串转换为真正的Json数组?

这是使用的代码:

$(document).on('click change', '.select-list', function(e) {
    e.preventDefault();
    var options = [];
    $('.optionitem').each(function(foo) {
        if($(this).is(':checked')) {
            options.push($(this).val());
        } else {
        }
    });
    $('#list').val(options);
    // This list is send via jQuery/Ajax to server and parsed like this when saving:
    // <?php $request['list'] = json_decode([$request['list']]); ?>
});

3 个答案:

答案 0 :(得分:1)

假设您有一个PHP字符串,并将其作为值...

通过json_decode运行它会将其转换为PHP字符串,其中包含一组以逗号分隔的JSON文本来表示对象。

使用[] 然后通过json_decode运行会将其转换为PHP数组。

<?php
    $original_json = <<<END
    "{\"id\":10,\"name\":\"Foobar\",\"sheep\":5,\"type\":\"test\",\"created_at\":\"2017-12-20 17:51:41\",\"updated_at\":\"2017-12-20 17:51:41\",\"title\":\"Sheep\"},{\"id\":13,\"name\":\"Foobar\",\"price\":5,\"type\":\"day\",\"created_at\":\"2017-12-21 18:02:28\",\"updated_at\":\"2017-12-21 18:02:28\",\"title\":\"Hello\"},{\"id\":15,\"name\":\"Car\",\"price\":5,\"type\":\"day\",\"created_at\":\"2018-03-16 11:16:59\",\"updated_at\":\"2018-03-16 11:16:59\",\"title\":\"Car\"}"
END;

    $json_one = "[" . json_decode($original_json, TRUE) . "]";
    $array = json_decode($json_one, TRUE);
    print_r($array);
?>

您的数据非常糟糕。您应该修复生成它的代码,而不是在此阶段将其破解为合理的东西。

答案 1 :(得分:0)

您可以使用stripslashes()echo

$json = "{\"id\":10,\"name\":\"Foobar\",\"sheep\":5,\"type\":\"test\",\"created_at\":\"2017-12-20 17:51:41\",\"updated_at\":\"2017-12-20 17:51:41\",\"title\":\"Sheep\"},{\"id\":13,\"name\":\"Foobar\",\"price\":5,\"type\":\"day\",\"created_at\":\"2017-12-21 18:02:28\",\"updated_at\":\"2017-12-21 18:02:28\",\"title\":\"Hello\"},{\"id\":15,\"name\":\"Car\",\"price\":5,\"type\":\"day\",\"created_at\":\"2018-03-16 11:16:59\",\"updated_at\":\"2018-03-16 11:16:59\",\"title\":\"Car\"}";
echo stripslashes($json);

从技术上讲,斜杠是用于转义的。内容中没有斜杠。如果有斜杠,则表示为:

"{\"Hello\": \"World \\ Oven\"}"

答案 2 :(得分:0)

您的字符串不是有效的JSON。我们假设您将字符串放入名为$data的变量:

$data = "[{\"id\":10,\"name\":\"Foobar\",\"sheep\":5,\"type\":\"test\",\"created_at\":\"2017-12-20 17:51:41\",\"updated_at\":\"2017-12-20 17:51:41\",\"title\":\"Sheep\"},{\"id\":13,\"name\":\"Foobar\",\"price\":5,\"type\":\"day\",\"created_at\":\"2017-12-21 18:02:28\",\"updated_at\":\"2017-12-21 18:02:28\",\"title\":\"Hello\"},{\"id\":15,\"name\":\"Car\",\"price\":5,\"type\":\"day\",\"created_at\":\"2018-03-16 11:16:59\",\"updated_at\":\"2018-03-16 11:16:59\",\"title\":\"Car\"}]";

即使您删除斜杠,它仍然无效:

json_decode(stripslashes($data)); // NULL

您应该修复JSON如何保存到数据库中以便开始使用有效的JSON。但是,如果您无法解决此问题,则可以将其转换为有效的JSON。它错过了周围的[]

json_decode(sprintf('[%s]', stripslashes($data)));

输出

array(3) { [0]=> object(stdClass)#1 (7) { ["id"]=> int(10) ["name"]=> string(6) "Foobar" ["sheep"]=> int(5) ["type"]=> string(4) "test" ["created_at"]=> string(19) "2017-12-20 17:51:41" ["updated_at"]=> string(19) "2017-12-20 17:51:41" ["title"]=> string(5) "Sheep" } [1]=> object(stdClass)#2 (7) { ["id"]=> int(13) ["name"]=> string(6) "Foobar" ["price"]=> int(5) ["type"]=> string(3) "day" ["created_at"]=> string(19) "2017-12-21 18:02:28" ["updated_at"]=> string(19) "2017-12-21 18:02:28" ["title"]=> string(5) "Hello" } [2]=> object(stdClass)#3 (7) { ["id"]=> int(15) ["name"]=> string(3) "Car" ["price"]=> int(5) ["type"]=> string(3) "day" ["created_at"]=> string(19) "2018-03-16 11:16:59" ["updated_at"]=> string(19) "2018-03-16 11:16:59" ["title"]=> string(3) "Car" } }

有关stripslashes的更多信息:http://php.net/manual/en/function.stripslashes.php