【问题标题】:PHP loop through JSON response and insert into MySQLPHP 循环遍历 JSON 响应并插入 MySQL
【发布时间】:2017-10-02 13:19:35
【问题描述】:

对于一个学校项目,我想遍历 JSON 响应并将数据插入 MySQL 数据库。

JSON 响应如下所示:

{"tableID":100965,"data":[{"updated_at":1506994152000,"prices":{"unstable_reason":"LOW_SALES","unstable":true,"sold":{"avg_daily_volume":null,"last_90d":67,"last_30d":52,"last_7d":15,"last_24h":1},"max":287.5,"avg":93.52,"min":51.01,"latest":87},"image":"IMGURL","db_name":"Test-Artikel","dbID":"123456789"},{"updated_at":1506994152000,"prices":{"unstable_reason":"LOW_SALES","unstable":true,"sold":{"avg_daily_volume":null,"last_90d":67,"last_30d":52,"last_7d":15,"last_24h":1},"max":287.5,"avg":93.52,"min":51.01,"latest":87},"image":"IMGURL","db_name":"Test-Artikel","dbID":"123456789"}],"__v":0,"createdAt":"2017-03-27T09:16:48.395Z"}

每个数组都应该是 mysql 数据库中自己的一行。

目前我可以使用以下代码从 json 文件中获取一个信息:

<?php
$url = "linktojson";

//connect to database
//$pdo = new PDO('mysql:host=localhost;dbname=...', '...', '...');

//read the json file contents
$jsondata = file_get_contents($url);

//convert json object to php associative array
$data = json_decode($jsondata, true);   

echo('<pre>');
foreach ($data['data'] as $api_data) {
    echo $api_data['name'] . '<br/>';

}
?>

编辑: 当前代码:

$pdo = new PDO('mysql:host=localhost;dbname=XXX', 'XXX', 'XXX');
$yourJsonArray ="test.json";
$dataArray = json_decode(json_encode($yourJsonArray),true);
foreach($dataArray as $key => $value){
   $image = $key['image'];
   $statement = $pdo->prepare('INSERT INTO api_data (image) VALUES (?)'); 
   $statement->execute(array($image));
}

错误消息:警告:在第 29 行的 test.php 中为 foreach() 提供的参数无效

第 29 行 = foreach(顶部有一些旧的 cmets)

编辑:在 foreach 之前添加 var_dump($yourJsonArray);

string(638) "{"tableID":100965,"data":[{"updated_at":1506994152000,"prices":{"unstable_reason":"LOW_SALES","unstable":true,"sold":{"avg_daily_volume":null,"last_90d":67,"last_30d":52,"last_7d":15,"last_24h":1},"max":287.5,"avg":93.52,"min":51.01,"latest":87},"image":"IMGURL","db_name":"Test-Artikel","dbID":"123456789"},{"updated_at":1506994152000,"prices":{"unstable_reason":"LOW_SALES","unstable":true,"sold":{"avg_daily_volume":null,"last_90d":67,"last_30d":52,"last_7d":15,"last_24h":1},"max":287.5,"avg":93.52,"min":51.01,"latest":87},"image":"IMGURL","db_name":"Test-Artikel","dbID":"123456789"}],"__v":0,"createdAt":"2017-03-27T09:16:48.395Z"}"
Warning: Invalid argument supplied for foreach() in test.php on line 30

编辑:将 $dataArray 更改为 $dataArray = json_decode($yourJsonArray,true);

错误 (4x): 警告:第 33 行 test.php 中的非法字符串偏移“图像”

第 33 行 = $image = $key['image'];

编辑:我转储了 $key Var 并得到了这个:

string(7) "tableID" string(4) "data" string(3) "__v" string(9) "createdAt"

如何获取“数据”的元素?

【问题讨论】:

  • 我知道如何循环遍历文件和每个数组中的一个元素(例如“名称”)。回声('
    '); foreach ($data['data'] as $name) { echo $name['name'] 。 '
    ';我找不到合适的方法来获取我需要的所有元素。
  • 你有'json响应'看起来像这样`'但那不是json。把真实的东西放在人们可以帮助你的地方,而不是你认为你得到的东西,或者你认为你在'linktojson'上生产的东西。
  • 好吧。我明白你的意思 - 将 json 文件的前两个元素添加到我的问题中。
  • ahhh ...他的眼睛已经看到了(并且jsonlint认为这个json是有效的)!因此,$api_data 数组似乎没有 name 属性。你是说db_name 吗?
  • 哈哈.. 我的问题中的 php 代码“已过时”-我现在使用 db_name。

标签: php mysql json loops foreach


【解决方案1】:

试试这样:

$yourJsonArray = "You Json Array will come here";
//$dataArray = json_decode(json_encode($yourJsonArray),true);
$dataArray = json_decode($yourJsonArray,true); 
foreach($dataArray as $key => $value){
   //Here 0,1,2,3 Will be contained inside the $key variable.
   //Code to insert the data comes here
}

【讨论】:

  • 谢谢 - 我会试一试!
  • 我试过了,但我得到了一个错误(见我上面的问题)
  • @FelixSchwie 在将其发送到foreach() 之前,您可以var_dump($yourJsonArray) 吗?
  • @FelixSchwie 我已经编辑了答案,删除了var_dump()
  • 向问题添加了新的 ErrorMSG
猜你喜欢
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
  • 2020-06-25
  • 2019-02-11
  • 1970-01-01
  • 2017-01-30
相关资源
最近更新 更多