获取PHP中的JSON对象,而不是数组

获取PHP中的JSON对象,而不是数组,php,json,Php,Json,我正在用php编写一个网站,从我创建的另一个php api中获取JSONstring。 字符串如下所示: { "result": "true", "results": { "20": { "id": "20", "desc": "a b ct tr", "active": "1", "startdate": "2013-04-03", "star

我正在用php编写一个网站,从我创建的另一个php api中获取JSONstring。 字符串如下所示:

{
    "result": "true",
    "results": {
        "20": {
            "id": "20",
            "desc": "a b ct tr",
            "active": "1",
            "startdate": "2013-04-03",
            "starttimehour": "18",
            "starttimemin": "0",
            "enddate": "2013-04-03",
            "endtimehour": "22",
            "endtimemin": "0",
            "creator": "a"
        },
        "21": {
            "id": "21",
            "desc": "test",
            "active": "0",
            "startdate": "2013-04-04",
            "starttimehour": "18",
            "starttimemin": "0",
            "enddate": "2013-04-04",
            "endtimehour": "22",
            "endtimemin": "0",
            "creator": "a"
        }
    }
}
{"result"="true", "results":[{...},{...},{...}]}
我找到了很多关于如何从JSONarray获取信息的答案,但我这里没有使用数组。 所以问题是:我如何获得标记为20、21等等的对象(这些数字是由服务器生成的,所以我不知道哪些会被返回)

或者我应该重写api如何将JSON作为数组返回。大概是这样的:

{
    "result": "true",
    "results": {
        "20": {
            "id": "20",
            "desc": "a b ct tr",
            "active": "1",
            "startdate": "2013-04-03",
            "starttimehour": "18",
            "starttimemin": "0",
            "enddate": "2013-04-03",
            "endtimehour": "22",
            "endtimemin": "0",
            "creator": "a"
        },
        "21": {
            "id": "21",
            "desc": "test",
            "active": "0",
            "startdate": "2013-04-04",
            "starttimehour": "18",
            "starttimemin": "0",
            "enddate": "2013-04-04",
            "endtimehour": "22",
            "endtimemin": "0",
            "creator": "a"
        }
    }
}
{"result"="true", "results":[{...},{...},{...}]}

我假设您是通过POST获得json,没有任何参数,比如

curl http://someapi.somedomain/someresource/ -X POST -d @data.json
所以基本上

$data = file_get_contents('php://input');
$object = json_decode($data);
print_r($object);

应该能解决你的问题。$object将是您发布的json对象。

您会得到一个字符串形式的json响应。这就是JSON的工作方式。要将数据“转换”为易于访问的格式和结构,可以使用名为
json\u decode()
的PHP函数

使用该函数时有两种选择-

  • 将数据转换为数组
    json\u解码($jsonString,true)

    如果使用此方法,将像访问关联数组一样访问数据<代码>$jsonArray['results']['21']

  • 将数据转换为对象<代码>json解码($jsonString)
    使用此方法,可以使用对象表示法遍历数据-
    $num=21
    $jsonObj->results->$num


  • 首先对字符串($string)进行解码,然后循环遍历该字符串并获取对象的所有属性。请记住,访问属性是使用->prop而不是['prop']。这样,您就不必以数组的方式处理它

    $jsoned = json_decode($string);
        foreach($jsoned->results as $o) {
            foreach($o as $key => $value) {
                echo "The key is: ".$key." and the value is: ".$value."<br>";
            }
        }
    
    $jsoned=json\u decode($string);
    foreach($jsoned->结果为$o){
    foreach($o作为$key=>$value){
    echo“键是:“.$key.”,值是:“.$value.”
    ”; } }
    工作示例将打印出什么:

    键为:id,值为:20

    键为:desc,值为:a b ct tr

    键为:活动,值为:1


    等等。

    不清楚您需要做什么。。。PHP和JSON使用术语“数组”来描述不同的事物。PHP的“数组”包括数字数组(JavaScript的
    []
    )和关联数组(JavaScript的
    {}
    )。使用
    json\u decode($input)
    将为您提供一个对象,使用
    json\u decode($input,true)
    将为您提供一个关联数组。不管怎样,我建议将
    结果
    作为数字数组返回。感谢所有的答案,它有助于json解析。另一个php页面还有另一个问题,它似乎在字符串中添加了一些东西,使得json_decode()返回NULL:Schange$json['results']到$json->results,但您的代码无论如何都无法工作。:)你是对的,我把所有的语法混在一起弄糊涂了。编辑已创建,为关联阵列添加了TRUE:-(