繁体   English   中英

如何自定义json响应?

[英]How to customize json response?

我请求一个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输出并以适当的表格格式显示它。

你正在迭代错误的对象,你需要遍历$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; ?>

您没有达到对象层次结构中的正确级别。 试试吧

foreach($obj->response->data->data as $licenseElement) { ... }
$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对象,这就是为什么我们需要“ - >”符号来引用它。

所有我改变的是foreach($ obj ['response'] - > data-> data as $ licenseElement)

我们还需要检查水平,这里我们是3级。:)

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM