php - 在php中解析Json时插入错误?

标签 php mysql arrays json

我收到此错误
错误:第 37 行路径名中的数组到字符串转换

<小时/>
<?php
  include('config.php');
    // read json file
if($_SERVER['REQUEST_METHOD'] == "GET"){
    $filename = 'employee.json';
    $json_data = file_get_contents($filename);   
    // $json_data=$_POST['QUESTION'];
    //convert json object to php associative array
    $data = json_decode($json_data,true);

if (is_array($data) || is_object($data))
{
    for($j=0;$j<sizeof($data);$j++){
        //$jsonData[$j] = $data[$j]['DATA'];
        //$jsonAnswers[$j] = $data[$j]['ANSWERS'];

        $drmobile = $data[$j]['DATA']['DRMOBILE'];
        $divcode = $data[$j]['DATA']['DIVCODE'];
        $brdcode = $data[$j]['DATA']['BRDCODE'];
        $prdcode = $data[$j]['DATA']['PRDCODE'];
        $createdon = $data[$j]['DATA']['CREATEDON'];

    //  echo $drmobile." -- ";
        for($i=0;$i<sizeof($data[$j]['ANSWERS']);$i++)
        {

            $quecode[$i] = $data[$j]['ANSWERS'][$i]['ADCODE'];
            $answer[$i] = $data[$j]['ANSWERS'][$i]['ANSWER'];

            // $quecodes=$quecode[$i];
            // $answers=$answer[$i];
            echo $quecode[$i]." <--> ".$answer[$i]."<br/>";
        }
        echo "<br/>-----------<br/>";
        $sql="INSERT INTO U_ANSWERS(DRMOBILE,ADCODE,DIVCODE,BRDCODE,PRDCODE,ANSWERS,CREATEDON)
        VALUES ('$drmobile', '$quecode', '$divcode', '$brdcode', '$prdcode', '$answer', '$createdon');";
            $qur = mysql_query($sql);
        if($qur){
        $json = array("status" => 1, "msg" => "Done User added!");
    }else{
        $json = array("status" => 2, "msg" => "Already Submitted");
    }


    }


}

}else{
    $json = array("status" => 0, "msg" => "Request method not accepted");
}


@mysql_close($conn);

/* Output header */
// header('Content-type: application/json');
echo json_encode($json);    
    //close connection
 ?>

示例 Json

[{
    "DATA": {
        "DRMOBILE": "8985180306",
        "DIVCODE": 2,
        "BRDCODE": 2,
        "PRDCODE": 2,
        "CREATEDON": "2017-08-10 11:38:22"
    },
    "ANSWERS": [{
        "ADCODE": 1,
        "ANSWER": "Excellent"
    }, {
        "ADCODE": 2,
        "ANSWER": "Yes"
    }, {
        "ADCODE": 3,
        "ANSWER": "No"
    }, {
        "ADCODE": 4,
        "ANSWER": "Yes"
    }, {
        "ADCODE": 5,
        "ANSWER": "Yes"
    }, {
        "ADCODE": 6,
        "ANSWER": "No"
    }, {
        "ADCODE": 7,
        "ANSWER": "2-4"
    }, {
        "ADCODE": 8,
        "ANSWER": "15"
    }]
},{
    "DATA": {
        "DRMOBILE": "8985180305",
        "DIVCODE": 1,
        "BRDCODE": 1,
        "PRDCODE": 1,
        "CREATEDON": "2017-08-10 11:38:22"
    },
    "ANSWERS": [{
        "ADCODE": 1,
        "ANSWER": "Excellent"
    }, {
        "ADCODE": 2,
        "ANSWER": "Yes"
    }, {
        "ADCODE": 3,
        "ANSWER": "No"
    }, {
        "ADCODE": 4,
        "ANSWER": "Yes"
    }, {
        "ADCODE": 5,
        "ANSWER": "Yes"
    }, {
        "ADCODE": 6,
        "ANSWER": "No"
    }, {
        "ADCODE": 7,
        "ANSWER": "2-4"
    }, {
        "ADCODE": 8,
        "ANSWER": "15"
    }]
},{
    "DATA": {
        "DRMOBILE": "8985180307",
        "DIVCODE": 2,
        "BRDCODE": 2,
        "PRDCODE": 2,
        "CREATEDON": "2017-08-10 11:38:22"
    },
    "ANSWERS": [{
        "ADCODE": 1,
        "ANSWER": "Excellent"
    }, {
        "ADCODE": 2,
        "ANSWER": "Yes"
    }, {
        "ADCODE": 3,
        "ANSWER": "No"
    }, {
        "ADCODE": 4,
        "ANSWER": "Yes"
    }, {
        "ADCODE": 5,
        "ANSWER": "Yes"
    }, {
        "ADCODE": 6,
        "ANSWER": "No"
    }, {
        "ADCODE": 7,
        "ANSWER": "2-4"
    }, {
        "ADCODE": 8,
        "ANSWER": "15"
    }]
}
]
<小时/>

最佳答案

在 for 循环中,它显示 $quecode$answer是数组。

在 SQL 语句中,您将它们用作字符串变量。

$sql="INSERT INTO U_ANSWERS(DRMOBILE,ADCODE,DIVCODE,BRDCODE,PRDCODE,ANSWERS,CREATEDON)
        VALUES ('$drmobile', '$quecode', '$divcode', '$brdcode', '$prdcode', '$answer', '$createdon');";

现在可能有两种可能的方法:

  1. 将此 SQL 语句也放入循环中,并插入所有单独的 这些数组中的值( $quecode$answer )

  2. 将这些( $quecode$answer )数组的值组合成一个字符串(例如,逗号 分隔的字符串)使用 implode功能如下:implode(",", $quecode);然后插入您现在使用的单个字符串。

关于php - 在php中解析Json时插入错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45607557/

相关文章:

php - echo 语句在 php 中的 while 循环中不起作用

mysql存储过程(带2个插入)错误

ruby - Ruby 方法能否作为迭代器产生或根据上下文返回数组?

php - 使用文本字段通过后端管理对前端菜单进行排序

r - 提高矩阵或数组的计算性能

c - 数组中的整数索引递增

php - Laravel 解析错误 : syntax error, unexpected T_CLASS, expecting T_STRING

php - 提交按钮不居中

php - 使用 Doctrine ODM 从 mongodb 文档中的数组中删除项目

mysql - 在 MySQL 中存储多语言地理数据