解码json响应并为每个插入使用

时间:2014-07-26 04:22:15

标签: php mysql json parsing

<?php
$string ='{"type":"Text","totalprice":"0.05","totalgsm":"6","remaincredit":"63.6433","messages": [
        {"status":"1","messageid":"157157","gsm":"9211111111111"},
        {"status":"1","messageid":"157154","gsm":"9211111111112"},
        {"status":"1","messageid":"157151","gsm":"9211111111113"},
        {"status":"1","messageid":"157149","gsm":"9211111111114"},
        {"status":"1","messageid":"157142","gsm":"9211111111115"},
        {"status":"1","messageid":"157130","gsm":"9211111111116"}
        ]}';
?>

我想得到:

type            = text
totalprice      = 0.05
totalgsm        = 6
remainingcredit = 63.6433

status=1 messageid=157157 gsm=9211111111111
status=1 messageid=157157 gsm=9211111111112
status=1 messageid=157157 gsm=9211111111113
status=1 messageid=157157 gsm=9211111111114

我想在2个查询中插入MYSQLDB:

type = text, totalprice = 0.05, totalgsm = 6, remainingcredit = 63.6433

的第一次INSERT查询

messages数组的第二次INSERT查询 使用foreach插入所有给定的

status=1 messageid=157157 gsm=9211111111111
status=1 messageid=157157 gsm=9211111111112
status=1 messageid=157157 gsm=9211111111113
status=1 messageid=157157 gsm=9211111111114

请指导我如何解码这种类型的json响应以及如何解析以在DB中插入?

3 个答案:

答案 0 :(得分:3)

你可以在php中使用json_decode()来解码这个字符串。

$data = json_decode($string);

答案 1 :(得分:0)

我认为您希望使用@myeishvar所述的json_decode

这会将您的JSON字符串转换为PHP数组。

$records = json_decode($string);
print_r($records); // Display the array contents

这将回显数组中每条记录所需的信息。

foreach ($records as $record) {
 echo 'Type: '.$record->type.'<br>';
 echo 'Total Price: '.$record->totalprice.'<br>';
 echo 'Total GSM: '.$record->totalgsm.'<br>';
 echo 'Remaining Credit: '.$record->remainingcredit.'<br>';
 echo '<br>';
}

这是使用foreach构建查询的示例,您可以使用该查询将值插入数据库表中的各个记录中。

$sql='INSERT INTO recordtable (type, totalprice, totalgsm, remainingcredit) VALUES ';
foreach ($records as $record) {
    $sql .= '('.$record->$type.', '.$record->$totalprice.', '.$record->$totalgsm.', '.$record->$remainingcredit.'), '
}
...do your SQL query...

我应该注意,如果这些数据来自用户输入(表格),则应在将其插入数据库之前对其进行适当的清理。

答案 2 :(得分:0)

愿你这样做:

$string = json_decode('{"type":"Text","totalprice":"0.05","totalgsm":"6","remaincredit":"63.6433","messages": [
    {"status":"1","messageid":"157157","gsm":"9211111111111"},
    {"status":"1","messageid":"157154","gsm":"9211111111112"},
    {"status":"1","messageid":"157151","gsm":"9211111111113"},
    {"status":"1","messageid":"157149","gsm":"9211111111114"},
    {"status":"1","messageid":"157142","gsm":"9211111111115"},
    {"status":"1","messageid":"157130","gsm":"9211111111116"}
    ]}', true);

$type = $string['type'];
$totalprice = $string['totalprice'];
$totalgsm = $string['totalgsm'];
$remaincredit = $string['remaincredit'];

/** Insert using First Query **/

/** Now for message array **/
foreach ($string['messages'] as $key => $msg) {
    $status = $msg['status'];
    $msg_id = $msg['messageid'];
    $gsm = $msg['gsm'];
    /*** Insert using your second query ***/
    /** ------------------------ **/
}