[英]How to loop through a JSON array and put data in variables
I am trying to get the data from an API and save it to a MySQL database. 我正在尝试从API获取数据并将其保存到MySQL数据库。 The problem is when I want to echo the data to check if it gets all the values I'm getting this error: 问题是当我想回显数据以检查它是否获取所有值时,出现此错误:
Notice: Trying to get property of non-object 注意:试图获取非对象的属性
My JSON data looks like this: 我的JSON数据如下所示:
{"AttractionInfo":[{"Id":"sprookjesbos","Type":"Attraction","MapLocation":"1","State":"open","StateColor":"green","WaitingTime":0,"StatePercentage":0},{"Id":"zanggelukkig","Type":"Show","MapLocation":".","State":"gesloten","StateColor":"clear"},
I think this is because it is in an array. 我认为这是因为它在数组中。 Because when I use this bit of code it gives no errors but I have to define the index of the array. 因为当我使用这段代码时,它不会给出任何错误,但是我必须定义数组的索引。 What is the best way to loop through my data and put it in variables? 遍历数据并将其放入变量的最佳方法是什么?
<?php
//Get content
$url = "https://eftelingapi.herokuapp.com/attractions";
$data = json_decode(file_get_contents($url));
//Fetch data
$attractieId = $data->AttractionInfo[0]->Id;
$attractieType = $data->AttractionInfo[0]->Type;
$attractieStatus = $data->AttractionInfo[0]->State;
$attractieStatusKleur = $data->AttractionInfo[0]->StateColor;
$attractieWachttijd = $data->AttractionInfo[0]->WaitingTime;
$attractieStatusPercentage = $data->AttractionInfo[0]->StatePercentage;
?>
I tried to find some solutions but all they use is a foreach loop. 我试图找到一些解决方案,但它们使用的只是一个foreach循环。 But i don't think that'll work right. 但是我认为那不会奏效。 Can anyone help me in the good direction or tell me how I might possibly fix this? 谁能在正确的方向帮助我,或者告诉我如何解决这个问题? I am not very experienced so any advice is welcome. 我不是很有经验,所以欢迎任何建议。 Thanks in advance. 提前致谢。
Update code: 更新代码:
require 'database-connection.php';
//Get content
$url = "https://eftelingapi.herokuapp.com/attractions";
$data = json_decode(file_get_contents($url));
//Fetch data
$attractieId = isset($data->AttractionInfo->Id);
$attractieType = isset($data->AttractionInfo->Type);
$attractieStatus = isset($data->AttractionInfo->State);
$attractieStatusKleur = isset($data->AttractionInfo->StateColor);
$attractieWachttijd = isset($data->AttractionInfo->WaitingTime);
$attractieStatusPercentage = isset($data->AttractionInfo->StatePercentage);
$sql = "INSERT INTO attracties (attractieId, attractieType, attractieStatus, attractieStatusKleur, attractieWachttijd, attractieStatusPercentage)
VALUES ('$attractieId', '$attractieType', '$attractieStatus', '$attractieStatusKleur', '$attractieWachttijd', '$attractieStatusPercentage')";
if ($db->query($sql) === TRUE) {
echo "success";
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
It says 'success' but when I look into my database it inserted only empty data. 它说“成功”,但是当我查看数据库时,它只插入了空数据。 I need to add all the attractions to my database so not just one row. 我需要将所有景点添加到数据库中,而不只是一行。 So I need to loop through my data. 所以我需要遍历我的数据。
try this... 尝试这个...
$content = json_decode(file_get_contents($url));
foreach($content->AttractionInfo as $data ){
$id = $data->Id;
$type = $data->Type;
$map = $data->MapLocation;
$state = $data->State;
$color = $data->StateColor;
if(!empty($data->WaitingTime)) {
$time = $data->WaitingTime;
}
if(!empty($data->StatePercentage)) {
$percent = $data->StatePercentage;
}
//persist your data into DB....
}
I think you need something like this: 我认为您需要这样的东西:
$json = '{"AttractionInfo":[{"Id":"sprookjesbos","Type":"Attraction","MapLocation":"1","State":"open","StateColor":"green","WaitingTime":0,"StatePercentage":0},{"Id":"zanggelukkig","Type":"Show","MapLocation":".","State":"gesloten","StateColor":"clear"}]}';
$arr = json_decode($json);
foreach ($arr->AttractionInfo as $key => $attraction) {
foreach($attraction as $key=> $value) {
print_r($key.' - '.$value.'<br>');
$$key = $value;
}
}
echo '<br>';
echo $Id; // it will be last item/attraction id.
We can improve this code. 我们可以改进此代码。 Just say where and how do you want to use it 只需说出您想在哪里以及如何使用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.