在php中将json字符串反斜杠到实际json数组

在php中将json字符串反斜杠到实际json数组,php,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\"

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

"{\"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']]); ?>
});
$(文档)。在('单击更改','上。选择列表',函数(e){
e、 预防默认值();
var期权=[];
$('.optionitem')。每个(函数(foo){
如果($(this).is(':checked')){
options.push($(this.val());
}否则{
}
});
$('列表').val(选项);
//此列表通过jQuery/Ajax发送到服务器,并在保存时按如下方式解析:
// 
});
您可以使用或
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\"}"

如果您有一个PHP字符串,并将其作为一个值

通过
json\u decode
运行它将把它转换为一个PHP字符串,其中包含一组表示对象的逗号分隔的json文本

[
]
包装它,然后通过
json\u 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);
?>



你的数据很糟糕。在这个阶段,您应该修复生成该字符串的代码,而不是将其破解成合理的代码。

您的字符串一开始就不是有效的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)));
输出
有关stripslashes的更多信息:

如何获得此信息?我建议您修复将此字符串存储到数据库中的代码,而不是尝试更正它now@user1469734您是如何获取数据以及如何使用数据的?@user1469734您是否尝试使用
JSON.parse()
?@PraveenKumar-问题已标记,不是“昆汀”看着问题,然后说话。谢谢
:D
$(document).ready()是jQuery,它是用JavaScript而不是PHP编写的。
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" } }