简体   繁体   中英

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.

I performed following php code to get array representation of JSON data.

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.

Please help me to parse the above json output and display it in proper tabular format.

you are iterating over wrong object, you need to loop over $obj->response->data->data object to get what you are seeking

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

It says StdClass Object thats why we need the "->" sign to refer to it.

All i have changed is the foreach($obj['response']->data->data as $licenseElement)

We also need to check the levels , here we are 3 levels in. :)

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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