我试图从json数组中获取两个变量的值。数组被发送到ajax,然后解码并“保存”到$jsonarray
中。然后我尝试从数组中获取volume
和symbol
变量,并将它们插入到我的数据库中。我不明白这个$jsonarray->result->{"quote"}->symbol
的语法,并且尝试过它如何正确,但是错误不会消失。
,那是我的数组:
{"query":{"count":1,"created":"2016-02-15T15:11:47Z","lang":"de-DE","results":{"quote":{"symbol":"ZN","Ask":"2.05","Bid":"1.78","Volume":"13214","PercentChange":"+0.56%"}}}}
相关的php文章:
$jsonString = $_POST['mydata'];
$jsonarray = json_decode($jsonString[0]['query']);
if ($stmt = $mysqli->prepare('INSERT INTO volume (stocksymbol, volume, time) VALUES ( ?, ?, now())')) {
/* bind parameters for markers */
$stmt->bind_param("si", $jsonarray->result->{"quote"}->symbol, $jsonarray->result->{"quote"}->Volume);
/* execute query */
$stmt->execute();
/* close statement */
$stmt->close();
}
发布于 2016-02-15 15:53:01
你确定这一行是对的吗?$jsonarray = json_decode($jsonString[0]['query']);
在这种情况下,您应该通过:$jsonarray->query->results->...
访问结果
发布于 2016-02-15 15:35:07
试着:
/* bind parameters for markers */
$stmt->bind_param("ss", $jsonarray->result->{"quote"}->symbol, $jsonarray->result->{"quote"}->Volume);
发布于 2016-02-16 04:30:21
您可以尝试将JSON解码为一个关联数组。假设$_POST['mydata']
包含您向我们展示的JSON字符串,请尝试如下:
$jsonString = $_POST['mydata'];
$jsonarray = json_decode($jsonString, TRUE);
这样,您可以以更一致的方式访问这些值:
$stmt->bind_param(
"si",
$jsonarray['query']['results']['quote']['symbol'],
$jsonarray['query']['results']['quote']['Volume']
);
https://stackoverflow.com/questions/35413046
复制相似问题