English 中文(简体)
JSON数据和PHP
原标题:JSON data and PHP
  • 时间:2012-04-30 19:22:09
  •  标签:
  • php
  • json

I am retrieving data via JSON and PHP from a URL. I am having difficulty breaking the object apart and displaying the data. The PHP code seems to be working until I get to the for loops.

$jsonurl =  http://myweb/api.php ;
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);
foreach ($json_output->Monitoring AS $monitoring) {
    foreach ($monitoring->Status AS $Status){
        echo $Status->Emailed;
        echo $Status->Status;
    }

我的数据结构如下:

 object(stdClass)#12 (1) 
      { ["Monitoring"]=> array(10) { [0]=> object(stdClass)#13 (14) 
            { 
            ["Status"]=> string(30) "HTTP/1.1 302 Moved Temporarily"
            ["Emailed"]=> string(1) "0" } 
            [1]=> object(stdClass)#14 (14) { 
            ["Status"]=> string(30) "HTTP/1.1 302 Moved Temporarily" 
            ["Emailed"]=> string(1) "0" } 
            [2]=> object(stdClass)#15 (14) { 
            ["Status"]=> string(30) "HTTP/1.1 302 Moved Temporarily" 
            ["Emailed"]=> string(1) "0" } 
            [3]=> object(stdClass)#16 (14) { 
            ["Status"]=> string(30) "HTTP/1.1 302 Moved Temporarily"               
            ["Emailed"]=> string(1) "0" }
            } } 
最佳回答

根据你所描述的数据结构,你只需要一个 for,例如:

$jsonurl =  http://myweb/api.php ;
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);
foreach ($json_output->Monitoring AS $Status) {
    echo $Status->Emailed;
    echo $Status->Status;
}

在第一个论坛中,价值(贵国法典中的监测、我刚刚改称的矿区中的薪等)不是你需要接手的另一阵列。 它含有一个带有电子邮件和作为钥匙的地位的 object物体。

问题回答

First of all, are you sure your script returns actual JSON string? Did you json_encode it?

您是否比较了在JSON和脱硫之后的物体数据结构? 是否有任何分歧? 也许这根本不是关于每个游乐场和初等动物的问题,而是属于你们的数据结构,而这种结构似乎包括多个次级目标。

Alternative is to try and use associative arrays instead of objects in that context by returning json_decode($json,true) and treating it as an array.

首先,确保你纠正错误报告,以便你能够看到究竟是失败了。

转至<代码>json_decode的不当格式将只是退回错误。 这是PHP s json处理工作的一个限制,他们试图通过创建<代码>JSONSerializable接口在下一期发行时处理。 确保回归代码为<代码>stdClass(或如果你通过联系阵列的选择,则阵列永远不会执行。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签