如何在不知道其名称(并且不在数组中)的情况下访问 JSON 对象

How to access JSON object without knowing its name (and that is not in an array)

我的问题很简单,至少我希望如此,但我就是找不到解决方案。我有一个 JSON 字符串,它是我从对服务器的获取请求中获得的。

我的例子 JSON :

"body":[
{
   "_id":"70:ee:50:05:00:aa",
   "place":{
      "location":[
         2.4358958,
         49.503012
      ],
      "altitude":89,
      "timezone":"Europe/Paris"
   },
   "mark":8,
   "measures":{
      "02:00:00:04:f8:6a":{
         "res":{
            "1431926951":[
               7.9,
               83
            ]
         },
      "type":[
         "temperature",
         "humidity"
         ]
      },
      "70:ee:50:05:00:aa":{
         "res":{
            "1431932787":[
               1009.4
            ]
         },
         "type":[
            "pressure"
         ]
      }
  },
  "modules":[
     "02:00:00:04:f8:6a"
  ]
},

我需要访问温度值,所以按照这个例子我会这样做:$value = body->measures->02:00:00:04:f8:6a->res->1431926951[0].

我知道我不能这样做:measures->02:00:00:04:f8:6a。很酷的是,这个困难可以通过这样做来解决:$module = body->modules[0] 然后我可以使用这个变量。 所以它看起来像这样:body->measures->$module->res->1431926951[0].

我似乎做不到的是最后一步:res->1431926951[0]。这个数字看起来完全是随机的我在 JSON 中的其他任何地方都找不到这个值所以我不能使用我为 02:00:00:04:f8:6a. 所做的相同技巧 所以我的问题是如何在不知道其名称的情况下访问位于对象“1431926951”内的数组的第一个值?

非常感谢您抽出时间来帮助我。

编辑:这是我在 PHP

中使用的代码
$results = json_decode($resp);
foreach($results->body as $result){
    $id = $result->_id;
    $lat = $result->place->location[0];
    $lng = $result->place->location[1];
    $module = $result->modules[0];
    $type = $result->measures->$module->type[0];
    $value = "1431926951";
    //$test = $result->measures->$module->res->$value[0];
    /*$res = $result->measures[$result->measures[0]]->res;
    $test = $res[Object.keys($res)[0]][0];*/
    echo 'Id: '.$id.' - Lat: '.$lat.' - Lng: '.$lng.' - test: '.$test."<br>";
}
var jsonvar = { key: { key1: "value 1", key2: "value 2" } };
var key = Object.keys(jsonvar)[0];

这就是我们如何使用 javascript 从给定的 json 对象中获取 json 键。

解释:假设您在 javascript 中有一个名为 jsonvar 的 json 变量,其中包含一些 json 数据.现在,要访问 parent json 中包含的第一个 key,然后使用 array,如上所示。

Object.keys(jsonvar)[0]

将 return json 键:"key"。我们正在访问 jsonvar 以访问对应于位置 [0].

的第一个键

希望这能解决您的问题。

您可以使用 Object.keys 访问 JavaScript 中 属性 的名称。

var res = obj.body.measures[ obj.body.modules[0] ].res;
console.log( res[ Object.keys(res)[0] ][0]); // prints 7.9

See demo

编辑(PHP 版本,只有 2 行):

与PHP相同,但使用key and stdClass (json_decode returns stdClass)

$res = $obj->body->measures->{ $obj->body->modules[0] }->res;
echo $res->{ key( (array) $res ) }[0]; // prints 7.9

See demo

希望得到帮助。

这是我在 PHP 中的做法:

<?php

$ob = '
{
"body":[
{
   "_id":"70:ee:50:05:00:aa",
   "place":{
      "location":[
         2.4358958,
         49.503012
      ],
      "altitude":89,
      "timezone":"Europe/Paris"
   },
   "mark":8,
   "measures":{
      "02:00:00:04:f8:6a":{
         "res":{
            "1431926951":[
               7.9,
               83
            ]
         },
      "type":[
         "temperature",
         "humidity"
         ]
      },
      "70:ee:50:05:00:aa":{
         "res":{
            "1431932787":[
               1009.4
            ]
         },
         "type":[
            "pressure"
         ]
      }
  },
  "modules":[
     "02:00:00:04:f8:6a"
  ]
}
]
}';

//turn the json into a PHP object
$ob = json_decode($ob);

//pre-defined array of known module names
$modules = array("02:00:00:04:f8:6a", "70:ee:50:05:00:aa");

//loop through each moduke
foreach($modules as $module){
    //get the 'res' property for that module
    $res = $ob->body[0]->measures->$module->res;
    //get a list of the object properties
    $properties = get_object_vars($res);
    //select the first property (we don't know the name) uising the current() function
    $firstProperty = current($properties);
    //and print it out
    echo "The first property of $module is: ";
    print_r($firstProperty);
    echo "<br/><br/>";
}