简体   繁体   English

如何自定义json响应?

[英]How to customize json response?

I request a JSON query and get the output, I want to parse that output and show it in tabular form also want to insert it into database. 我请求一个JSON查询并获取输出,我想解析该输出并以表格形式显示它也想将其插入数据库。

I performed following php code to get array representation of JSON data. 我执行以下PHP代码来获取JSON数据的数组表示。

echo '<pre>';
print_r( json_decode( $result ) );
echo '</pre>';

and I get following output: 我得到以下输出:

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] => 
    )

)

I want to display above data into tabular form. 我想将上面的数据显示为表格形式。

Code used by me: 我使用的代码:

$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; ?>

This code returns me an error Trying to get property of non-object everywhere at echo syntax. 此代码返回错误尝试在echo语法中获取非对象的属性

Please help me to parse the above json output and display it in proper tabular format. 请帮我解析上面的json输出并以适当的表格格式显示它。

you are iterating over wrong object, you need to loop over $obj->response->data->data object to get what you are seeking 你正在迭代错误的对象,你需要遍历$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; ?>

You are not getting to the right level in the object hierarchy. 您没有达到对象层次结构中的正确级别。 Try with 试试吧

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; ?>

This code should work. 这段代码应该有效。 What you are trying to do is that you have considered the response as an array but the print_r functions lets us know wether your data is an array or an object. 您要做的是您已将响应视为数组,但print_r函数让我们知道您的数据是数组还是对象。

It says StdClass Object thats why we need the "->" sign to refer to it. 它说StdClass对象,这就是为什么我们需要“ - >”符号来引用它。

All i have changed is the foreach($obj['response']->data->data as $licenseElement) 所有我改变的是foreach($ obj ['response'] - > data-> data as $ licenseElement)

We also need to check the levels , here we are 3 levels in. :) 我们还需要检查水平,这里我们是3级。:)

Hope this helps. 希望这可以帮助。

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

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