dongmeixi5311 2014-06-12 18:39
浏览 14
已采纳

如何在php中管理json数据

I called JSON data and got following output:

stdClass Object
(
[request] => stdClass Object
    (
        [Target] => Affiliate_Report
        [Format] => json
        [Service] => HasOffers
        [Version] => 3
        [Method] => getConversions
        [api_key] => my_key
        [NetworkId] => icubes
        [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] => 100
                [count] => 75
                [pageCount] => 1
                [data] => Array
                    (
                        [0] => stdClass Object
                            (
                                [Offer] => stdClass Object
                                    (
                                        [name] => Myntra (CPS)
                                    )

                                [Browser] => stdClass Object
                                    (
                                        [display_name] => Chrome
                                    )

                                [Stat] => stdClass Object
                                    (
                                        [payout] => 150.00000
                                        [sale_amount] => 954.00000
                                        [status] => rejected
                                        [datetime] => 2014-03-31 19:49:50
                                        [ip] => 103.226.84.249
                                        [ad_id] => 102ecc5fe230883c195d8a0e84ef7f
                                        [affiliate_info1] => 
                                    )

                            )

                        [1] => stdClass Object
                            (
                                [Offer] => stdClass Object
                                    (
                                        [name] => Myntra (CPS)
                                    )

                                [Browser] => stdClass Object
                                    (
                                        [display_name] => Firefox
                                    )

                                [Stat] => stdClass Object
                                    (
                                        [payout] => 270.00000
                                        [sale_amount] => 545.00000
                                        [status] => approved
                                        [datetime] => 2014-04-18 12:00:20
                                        [ip] => 27.0.51.119
                                        [ad_id] => 10256740541d68b117955aa58529a6
                                        [affiliate_info1] => 
                                    )

                            )

I want to display that data in tabular form using table tag and also want to insert that data into a MySQL database.

But I dont understand the array format of data.

My code:

$result = file_get_contents($base);

$obj = json_decode($result);
echo"<table>";
foreach($obj as $item) {
echo"
<tr>
<td>$item['Offer.name']</td>
<td>$item['Browser.display_name']</td>
<td>$item['Stat.payout']</td>
<td>$item['Stat.sale_amount']</td>
<td>$item['Stat.datetime']</td>
<td>$item['Stat.ip']</td>
</tr>
"; 
}
echo"</table>";

This code gives me a blank output.


i used following code and got output but some errors are still there.

$result = file_get_contents($base);

$obj = json_decode($result, true);
echo"<table border=1>";
$i = 0;
    foreach($obj['response'] as $item) {

    for($i=0;$i<=10;$i++)
    {
        echo"
        <tr>
        <td>{$item['data'][$i]['Offer']['name']}</td>
        <td>{$item['data'][$i]['Stat']['payout']}</td>
        <td>{$item['data'][$i]['Stat']['sale_amount']}</td>
        <td>{$item['data'][$i]['Stat']['status']}</td>
        <td>{$item['data'][$i]['Stat']['datetime']}</td>
        <td>{$item['data'][$i]['Stat']['ip']}</td>
        </tr>
        "; 
    }

    }
echo"</table>";

my output:

enter image description here

why upper rows are displaying blank values when there is no blank data is present?

and what are those errors?

  • 写回答

2条回答 默认 最新

  • dqrsceg6279196 2014-06-13 14:30
    关注

    You're using the wrong syntax. It's difficult to see exactly the results you're expecting, the values of the objects being returned look like they are objects of the original json themselves and without any useful data. However, to clarify for you:

    You're returning the json_decode() results as an object and trying to access it as an array. To return an associative array you need to add the 'assoc' parameter to the function, to read:

    json_decode($result, true)
    

    and you were trying to eco out the value directly from the associative array, you need to access it via the key, which will give you the value to read:

    $result = file_get_contents($base);
    
    $obj = json_decode($result, true);
    echo"<table>";
    foreach($obj['request'] as $item) {
    echo"
    <tr>
    <td>{$item['fields'][0]}</td>
    <td>{$item['fields'][1]}</td>
    <td>{$item['fields'][2]}</td>
    <td>{$item['fields'][3]}</td>
    <td>{$item['fields'][4]}</td>
    <td>{$item['fields'][5]}</td>
    </tr>
    "; 
    }
    echo"</table>";
    

    Also, I've added the curly brackets inside the string to escape the variable expressions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line