[英]read json data in php
I've created a valid JSON object in JavaScript: 我在JavaScript中创建了一个有效的JSON对象:
[
{
"code": "2F-58S",
"price": "123,13"
},
{
"code": "2F-58S",
"price": "123,13"
}
]
I have a problem with reading it in PHP: 我在PHP中阅读时遇到了问题:
$productsArr = json_decode($_GET['object']);
foreach($productsArr as $article)
{
$html .='<td>'.$article->code.'</td>';
$html .='<td>'.$article->price.'</td></tr>';
}
I'm getting an error that foreach()
loop is not defined as it should be. 我收到一个错误,因为未正确定义
foreach()
循环。 What am I missing in here? 我在这里想念什么?
EDIT: 编辑:
It seems that I'm not receiving an entire JSON values with the GET method. 看来我没有通过GET方法接收到整个JSON值。 I'm getting something like:
[{"code":"2F-58S","price":"123,13"},{"code":
我收到类似以下内容的信息:
[{"code":"2F-58S","price":"123,13"},{"code":
EDIT2: 编辑2:
JSON object break
is happening when reading HTML tags from the object, error is created when you reach this html tag: <p><strong>EnMotion </strong></p>\\n\\n<p><strong>impulse</strong></p>
从对象读取HTML标记时,发生JSON对象
break
,到达该html标记时会产生错误: <p><strong>EnMotion </strong></p>\\n\\n<p><strong>impulse</strong></p>
try this: 尝试这个:
$productsArr = file_get_contents('php://input');
foreach($productsArr as $article)
{
$html .='<td>'.$article->code.'</td>';
$html .='<td>'.$article->price.'</td></tr>';
}
Try true, with json_decode. 使用json_decode尝试为true。
if(!empty($_GET['object'])){
$productsArr = json_decode($_GET['object'],true);// this will return array
}
Working: 工作方式:
<?php
$a = '[
{
"code": "2F-58S",
"price": "123,13"
},
{
"code": "2F-58S",
"price": "123,13"
} ]';
$productsArr = json_decode($a,true);
print_r($productsArr);
$html = '';
foreach($productsArr as $article) {
$html .='<td>'.$article['code'].'</td>';
$html .='<td>'.$article['price'].'</td></tr>';
echo $html;
}
?>
Output: 输出:
<td>2F-58S</td><td>123,13</td></tr><td>2F-58S</td><td>123,13</td></tr>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.