PHP中的PHP Json数组解码并插入mysql

时间:2016-01-21 05:47:42

标签: php mysql json

"SCI-6":{"quantity":11,"id":"SCI-6","price":15,"name":"notebooks"},
"SCI-7":{"quantity":1,"id":"SCI-7","price":10,"name":"posters"},
"SCI-8":{"quantity":2,"id":"SCI-8","price":15,"name":"pen"},
"SCI-9":{"quantity":4,"id":"SCI-9","price":100,"name":"charger"},
"SCI-10":{"quantity":1,"id":"SCI-10","price":10.25,"name":"News Paper"}

请帮我解码变量并使用php

将值插入Mysql

5 个答案:

答案 0 :(得分:3)

使用string message = "Your purchase of $ 100.00 is awaiting for confirmation. Please use PIN 8967 to complete the transaction. Reference Number :1237689"; string output = Regex.Replace(message, @"(?<=PIN\s*)\d+", m => new string('*', m.Length)); - 解码JSON字符串

尝试如下:

json_decode

你得到的输出是:

<?php 
$json = '{"SCI-6":{"quantity":11,"id":"SCI-6","price":15,"name":"notebooks"},"SCI-7":{"quantity":1,"id":"SCI-7","price":10,"name":"posters"},"SCI-8":{"quantity":2,"id":"SCI-8","price":15,"name":"pen"},"SCI-9":{"quantity":4,"id":"SCI-9","price":100,"name":"charger"},"SCI-10":{"quantity":1,"id":"SCI-10","price":10.25,"name":"News Paper"}}';
$result_data = json_decode($json);
foreach($result_data as $value)
{
    $stmt = mysqli_prepare("INSERT INTO tablename (quantity, id, price, name) (?, ?, ?, ?)");
    mysqli_stmt_bind_param($stmt, "dsss", $value->quantity, $value->id, $value->price, $value->name);
    mysqli_stmt_execute($stmt);
}
?>

答案 1 :(得分:1)

$data = '{"SCI-6":{"quantity":11,"id":"SCI-6","price":15,"name":"notebooks"},"SCI-7":{"quantity":1,"id":"SCI-7","price":10,"name":"posters"},"SCI-8":{"quantity":2,"id":"SCI-8","price":15,"name":"pen"},"SCI-9":{"quantity":4,"id":"SCI-9","price":100,"name":"charger"},"SCI-10":{"quantity":1,"id":"SCI-10","price":10.25,"name":"News Paper"}}';

$json_data=json_decode($data);
foreach($json_data as $json_value)
{

    mysql_query("INSERT INTO yourtable (quantity, id, price, name) values ('".$json_value->quantity."','".$json_value->id."','".$json_value->price."','".$json_value->name."')");

}

答案 2 :(得分:0)

假设你的json字符串是

$dat = '{"SCI-6":{"quantity":11,"id":"SCI-6","price":15,"name":"notebooks"},"SCI-7":{"quantity":1,"id":"SCI-7","price":10,"name":"posters"},"SCI-8":{"quantity":2,"id":"SCI-8","price":15,"name":"pen"},"SCI-9":{"quantity":4,"id":"SCI-9","price":100,"name":"charger"},"SCI-10":{"quantity":1,"id":"SCI-10","price":10.25,"name":"News Paper"}}';

结尾处的开始和结束括号(否则它是无效的json)然后我们可以解码它如下:

$jsondata = json_decode($dat, true);
foreach($jsondata as $vals)
{
    $stmt = mysqli_prepare("INSERT INTO yourtable (quantity, id, price, name) (?, ?, ?, ?)");
    mysqli_stmt_bind_param($stmt, "dsss", $vals['quantity'], $vals['id'], $vals['price'], $vals['name']);
    mysqli_stmt_execute($stmt);
}

答案 3 :(得分:0)

您可以使用json_decode函数解码json:

$string = '{"SCI-6":{"quantity":11,"id":"SCI-6","price":15,"name":"notebooks"},"SCI-7":{"quantity":1,"id":"SCI-7","price":10,"name":"posters"},"SCI-8":{"quantity":2,"id":"SCI-8","price":15,"name":"pen"},"SCI-9":{"quantity":4,"id":"SCI-9","price":100,"name":"charger"},"SCI-10":{"quantity":1,"id":"SCI-10","price":10.25,"name":"News Paper"}}';

echo "<pre>";    
print_r(json_decode($string));

<强>结果:

stdClass Object
(
    [SCI-6] => stdClass Object
        (
            [quantity] => 11
            [id] => SCI-6
            [price] => 15
            [name] => notebooks
        )

    [SCI-7] => stdClass Object
        (
            [quantity] => 1
            [id] => SCI-7
            [price] => 10
            [name] => posters
        )

    [SCI-8] => stdClass Object
        (
            [quantity] => 2
            [id] => SCI-8
            [price] => 15
            [name] => pen
        )

    [SCI-9] => stdClass Object
        (
            [quantity] => 4
            [id] => SCI-9
            [price] => 100
            [name] => charger
        )

    [SCI-10] => stdClass Object
        (
            [quantity] => 1
            [id] => SCI-10
            [price] => 10.25
            [name] => News Paper
        )

)

旁注:

您的json包含多维数组,因此您需要在json内使用{}

答案 4 :(得分:-1)

您可以使用json_decode()解码json数据。

如果它不起作用,您也可以尝试unserialize()