我请求JSON查询并获取输出,我想解析该输出并以表格形式显示它也想将其插入数据库。
我执行了以下php代码来获取JSON数据的数组表示。
echo '<pre>';
print_r( json_decode( $result ) );
echo '</pre>';
我得到以下输出:
stdClass Object
(
[request] => stdClass Object
(
[Target] => Affiliate_Report
[Format] => json
[Service] => HasOffers
[Version] => 3
[Method] => getConversions
[api_key] =>
[NetworkId] =>
[limit] => 2
[fields] => Array
(
[0] => Offer.name
[1] => Browser.display_name
[2] => Stat.payout
[3] => Stat.sale_amount
[4] => Stat.status
[5] => Stat.datetime
[6] => Stat.ip
[7] => Stat.ad_id
[8] => Stat.affiliate_info1
)
)
[response] => stdClass Object
(
[status] => 1
[httpStatus] => 200
[data] => stdClass Object
(
[page] => 1
[current] => 2
[count] => 81
[pageCount] => 41
[data] => Array
(
[0] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Firefox
)
[Stat] => stdClass Object
(
[payout] => 150.00000
[sale_amount] => 0.00000
[status] => approved
[datetime] => 2014-05-20 22:20:05
[ip] => 27.0.50.82
[ad_id] => 102fa12e74df6018e502d8e152adb2
[affiliate_info1] =>
)
)
[1] => stdClass Object
(
[Offer] => stdClass Object
(
[name] => Myntra (CPS)
)
[Browser] => stdClass Object
(
[display_name] => Firefox
)
[Stat] => stdClass Object
(
[payout] => 150.00000
[sale_amount] => 53.00000
[status] => rejected
[datetime] => 2014-03-30 13:14:50
[ip] => 27.0.51.145
[ad_id] => 102be1d682ac9b2e9ee8e14dd1aeca
[affiliate_info1] =>
)
)
)
[dbSource] => branddb
)
[errors] => Array
(
)
[errorMessage] =>
)
)
我想将上述数据显示为表格形式。
我使用的代码:
$result = file_get_contents($base);
$obj = json_decode($result, true);
<?php foreach ($obj['response'] as $licenseElement) :?>
<tr>
<td><?php echo $licenseElement->data->Offer->name; ?></td>
<td><?php echo $licenseElement->Stat->payout; ?></td>
<td><?php echo $licenseElement->Stat->sale_amount; ?></td>
<td><?php echo $licenseElement->Stat->datetime; ?></td>
</tr>
<?php endforeach; ?>
此代码在echo语法处返回错误尝试获取非对象的属性。
请帮我解析上面的json输出并以适当的表格格式显示。
答案 0 :(得分:2)
你正在迭代错误的对象,你需要遍历$obj->response->data->data
对象来获得你想要的东西
<?php foreach ($obj->response->data->data as $licenseElement) :?>
<tr>
<td><?php echo $licenseElement->Offer->name; ?></td>
<td><?php echo $licenseElement->Stat->payout; ?></td>
<td><?php echo $licenseElement->Stat->sale_amount; ?></td>
<td><?php echo $licenseElement->Stat->datetime; ?></td>
</tr>
<?php endforeach; ?>
答案 1 :(得分:1)
您没有达到对象层次结构中的正确级别。试试
foreach($obj->response->data->data as $licenseElement) { ... }
答案 2 :(得分:0)
$result = file_get_contents($base);
$obj = json_decode($result, true);
<?php foreach ($obj['response']->data->data as $licenseElement) :?>
<tr>
<td><?php echo $licenseElement->data->Offer->name; ?></td>
<td><?php echo $licenseElement->Stat->payout; ?></td>
<td><?php echo $licenseElement->Stat->sale_amount; ?></td>
<td><?php echo $licenseElement->Stat->datetime; ?></td>
</tr>
<?php endforeach; ?>
此代码应该有效。 您要做的是您已将响应视为数组,但print_r函数让我们知道您的数据是数组还是对象。
它说StdClass对象就是为什么我们需要“ - &gt;”签名来引用它。
所有我改变的是foreach($ obj ['response'] - &gt; data-&gt;数据为$ licenseElement)
我们还需要检查水平,这里我们是3级。:)
希望这有帮助。