抖动http的JSON格式

时间:2018-07-26 16:51:01

标签: php mysqli flutter

长话短说,我在MySQL数据库中输入了json_object。使用PHP,我为flutter项目创建了一个Http。但是我收到显示以下错误:

FormatException: Unexpected character (at line 7, character 26)
        "attributes": "{"screen": "50 inch", "resolution": "2048 x 1152 pix...
                         ^

我到http的php代码

$search = "SELECT * FROM `database`.`products` 
            WHERE `category_id` = 1 
            AND JSON_EXTRACT(`attributes` , '$.ports.usb') > 0 
            AND JSON_EXTRACT(`attributes` , '$.ports.hdmi') > 0;";

$result = mysqli_query($link, $search);

if ($result) {
    //Printing out the details of the rows

    while($array = mysqli_fetch_assoc($result)) {
        $jsonData[]=$array;
    }

    $nonArr = json_encode($jsonData, JSON_PRETTY_PRINT);
    $stringArr = (string)$nonArr;
    $nonSlash = str_replace("\\","", $stringArr);
    echo $nonSlash;
}  

mysqli_close($link)

这是http显示的内容

[
    {
        "id": "1",
        "name": "Prime",
        "brand_id": "1",
        "category_id": "1",
        "attributes": "{"screen": "50 inch", "resolution": "2048 x 1152 pixels", "ports": {"hdmi": 1, "usb": 3}, "speakers": {"left": "10 watt", "right": "10 watt"}}"
    },
    {
        "id": "2",
        "name": "Octoview",
        "brand_id": "1",
        "category_id": "1",
        "attributes": "{"screen": "40 inch", "resolution": "1920 x 1080 pixels", "ports": {"hdmi": 1, "usb": 2}, "speakers": {"left": "10 watt", "right": "10 watt"}}"
        }
]

看起来不错吗?不是。

仅当我在cpanel示例中使用指向json文件的url(something.json文件)并按如下所示键入每一行时,flutter项目才起作用:

[
    {
        "id": "1",
        "name": "Prime",
        "brand_id": "1",
        "category_id": "1",
        "attributes": [{"screen": "50 inch", "resolution": "2048 x 1152 pixels", "ports": {"hdmi": 1, "usb": 3}, "speakers": {"left": "10 watt", "right": "10 watt"}]
    },
    {
        "id": "2",
        "name": "Octoview",
        "brand_id": "1",
        "category_id": "1",
        "attributes": [{"screen": "40 inch", "resolution": "1920 x 1080 pixels", "ports": {"hdmi": 1, "usb": 2}, "speakers": {"left": "10 watt", "right": "10 watt"}}]
        }
]

如您所见,区别只是将“”更改为[]。 那里的专业人士会告诉我如何获得此结果?

0 个答案:

没有答案