如何在php中管理json数据
我调用了JSON数据并得到以下输出:如何在php中管理json数据,php,mysql,arrays,json,Php,Mysql,Arrays,Json,我调用了JSON数据并得到以下输出: stdClass Object ( [request] => stdClass Object ( [Target] => Affiliate_Report [Format] => json [Service] => HasOffers [Version] => 3 [Method] => getConversions [
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] =>
)
)
我想使用table标记以表格形式显示数据,还想将数据插入MySQL数据库
但是我不理解数据的数组格式
我的代码:
$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>";
$result=file\u get\u contents($base);
$obj=json_decode($result);
回声“;
foreach($obj作为$item){
回声“
$item['Offer.name']
$item['Browser.display_name']
$item['Stat.payout']
$item['Stat.sale\u amount']
$item['Stat.datetime']
$item['Stat.ip']
";
}
回声“;
这段代码给我一个空白输出
我使用了下面的代码并得到了输出,但仍然存在一些错误
$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>";
$result=file\u get\u contents($base);
$obj=json_decode($result,true);
回声“;
$i=0;
foreach($obj['response']作为$item){
对于($i=0;$i,因为您在响应中得到了对象,请首先尝试json\u解码($result->request)
,通常不要像通常使用数组一样使用[]
编辑:尝试按如下方式输出响应:
echo '<pre>';
print_r($your-variable-here);
echo '</pre>';
echo';
打印(此处为您的变量);
回声';
然后让我知道您使用了错误的语法。很难准确地看到您期望的结果,返回的对象的值看起来像原始json的对象,并且没有任何有用的数据。但是,为您澄清一下:
您将以对象的形式返回json\u decode()
结果,并尝试以数组的形式访问它。
要返回关联数组,需要将“assoc”参数添加到函数中,以读取:
$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>";
如果您试图直接从关联数组中提取值,则需要通过键访问该值,该键将为您提供要读取的值:
$result=file\u get\u contents($base);
$obj=json_decode($result,true);
回声“;
foreach($obj['request']作为$item){
回声“
{$item['fields'][0]}
{$item['fields'][1]}
{$item['fields'][2]}
{$item['fields'][3]}
{$item['fields'][4]}
{$item['fields'][5]}
";
}
回声“;
此外,我还在字符串中添加了花括号以转义变量表达式。请发布var\u dump($obj)的结果
;@RobertRozas他这样做了…作为他自己问题的答案,而不是将其添加到问题本身。无法获取。我想以表格形式显示json生成的数据,但我无法理解数组的结构,并且获取未定义索引的错误:Offer.name我在问题中提到的输出是print\r的输出(json_decode($result))我尝试了您的代码,但它给了我一个错误致命错误:无法将字符串偏移量用作数组($item['fields'][0])
$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>";