我设计了一个Android应用程序,我将json中的数据发送到服务器上的php文件。 Json是:
[
{
"user_id":"11",
"check_id":"38",
"pcode_id":"14",
"platform_id":"2",
"vin":"MA11340DP0DN09661",
"date":"2017-06-09-10-48-25",
"status":"completed",
"description":"Check for proper insert of the connector and loose",
"result":true
},
{
"user_id":"11",
"check_id":"39",
"pcode_id":"14",
"platform_id":"2",
"vin":"MA11340DP0DN09661",
"date":"2017-06-09-10-48-25",
"status":"completed",
"description":"Damaged\/Cracked",
"result":false
}]
现在我必须读取这些数据,然后在php文件中逐个放入数组。这样我就可以将它和inesert计算到适当的表格中。 请帮我。使用post方法和json编码的应用程序。我的php文件代码是
$json = json_decode(file_get_contents("php://input"));
print_r($json);
foreach($json as $a)
echo $a['user_id'];{
echo $a['check_id'];
echo $a['pcode_id'];
echo $a['platform_id'];
echo $a['date'];
echo $a['status'];
echo $a['description'];
echo $a['result'];
}
答案 0 :(得分:1)
您正尝试将JSON属性作为数组键进行访问。但你拥有的是StdObject,所以你必须使用 - >。
我们假设您有一个这样的网址:http://mydata.data/somefile.php这会吐出JSON数据。您所要做的就是:
$ data - file_get_contents(' http://mydata.data/somefile.php');
请试一试:
<?php
$data = '[
{
"user_id":"11",
"check_id":"38",
"pcode_id":"14",
"platform_id":"2",
"vin":"MA11340DP0DN09661",
"date":"2017-06-09-10-48-25",
"status":"completed",
"description":"Check for proper insert of the connector and loose",
"result":true
},
{
"user_id":"11",
"check_id":"39",
"pcode_id":"14",
"platform_id":"2",
"vin":"MA11340DP0DN09661",
"date":"2017-06-09-10-48-25",
"status":"completed",
"description":"Damaged\/Cracked",
"result":false
}]';
$json = json_decode($data);
echo '<pre>';
print_r($json);
echo '</pre>';
echo '<br><br>loop';
foreach($json as $a) {
echo $a->user_id;
echo $a->check_id;
echo $a->pcode_id;
echo $a->platform_id;
echo $a->date;
echo $a->status;
echo $a->description;
echo $a->result;
}
答案 1 :(得分:0)
json_decode
解码json并作为对象返回。如果您想要数组,则将true
作为第二个参数传递
$json = json_decode(file_get_contents("php://input"),true);
print_r($json);
foreach($json as $a)
{
echo $a['user_id'];
echo $a['check_id'];
echo $a['pcode_id'];
echo $a['platform_id'];
echo $a['date'];
echo $a['status'];
echo $a['description'];
echo $a['result'];
}
答案 2 :(得分:0)
默认情况下json_decode
输出对象而不是数组,如果需要将数组作为true
中的输出传递json_decode
作为
$json = json_decode(file_get_contents("php://input"),true);
print_r($json);
foreach($json as $a)
echo $a['user_id'];{
echo $a['check_id'];
echo $a['pcode_id'];
echo $a['platform_id'];
echo $a['date'];
echo $a['status'];
echo $a['description'];
echo $a['result'];
}
答案 3 :(得分:0)