如何访问JSON对象而不知道它的名称(这不在数组中)

时间:2022-05-15 04:45:12

My problem is rather simple, at least I hope so, but I just cannot find the solution. I have a JSON string, which I got from a get request to a server.

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

Exemple of my 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"
  ]
},

I need to accessthe temperature value, so following this example I would do this : $value = body->measures->02:00:00:04:f8:6a->res->1431926951[0].

我需要访问温度值,因此按照这个示例,我将这样做:$value = body->度量->02:00:00:04:f8:6a->res->1431926951[0]。

I know i can't just do this : measures->02:00:00:04:f8:6a. The cool thing is that this difficulty can be solved doing this : $module = body->modules[0] and then I can use this variable. So it would look like this : body->measures->$module->res->1431926951[0].

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

What I can't seem to do though is the final step : res->1431926951[0]. This number seem completely random I can't find this value anywhere else in the JSON so I can't use the same trick I did for 02:00:00:04:f8:6a. So my question is how can I access the first value of the array located inside the object "1431926951" without knowing its name ?

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

Thank you very much for taking the time to help me.

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

EDIT : here is the code I am using in PHP

编辑:这是我在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>";
}

3 个解决方案

#1


2  

You can access with the Object.keys to the name of the property in JavaScript.

您可以访问该对象。JavaScript中属性名称的键。

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

See demo

看到演示

EDIT (PHP version, only 2 lines):

编辑(PHP版本,仅2行):

With PHP is the same but using key and stdClass (json_decode returns stdClass)

使用PHP也是一样的,但是使用key和stdClass (json_decode返回stdClass)

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

See demo

看到演示

Hope help.

希望有帮助。

#2


1  

Here's how I did it in PHP:

下面是我在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/>";
}

#3


1  

var jsonvar = { key: { key1: "value 1", key2: "value 2" } };
var key = Object.keys(jsonvar)[0];

This is how we can get the json key from the given json object using javascript.

这就是使用javascript从给定的json对象中获取json键的方法。

Explaination: Lets say you have a json variable in javascript named jsonvar which is holding some json data. Now, to access the first key contained in parent json, then make use of array as shown above.

解释:假设在javascript中有一个名为jsonvar的json变量,它包含一些json数据。现在,要访问父json中包含的第一个键,请使用上面所示的数组。

Object.keys(jsonvar)[0]

种(jsonvar)[0]

will return the json key: "key". We are accessing the jsonvar to access the first key which corresponds to position [0].

将返回json键:“key”。我们正在访问jsonvar以访问与位置[0]对应的第一个键。

Hope this could solve your problem.

希望这能解决你的问题。

#1


2  

You can access with the Object.keys to the name of the property in JavaScript.

您可以访问该对象。JavaScript中属性名称的键。

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

See demo

看到演示

EDIT (PHP version, only 2 lines):

编辑(PHP版本,仅2行):

With PHP is the same but using key and stdClass (json_decode returns stdClass)

使用PHP也是一样的,但是使用key和stdClass (json_decode返回stdClass)

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

See demo

看到演示

Hope help.

希望有帮助。

#2


1  

Here's how I did it in PHP:

下面是我在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/>";
}

#3


1  

var jsonvar = { key: { key1: "value 1", key2: "value 2" } };
var key = Object.keys(jsonvar)[0];

This is how we can get the json key from the given json object using javascript.

这就是使用javascript从给定的json对象中获取json键的方法。

Explaination: Lets say you have a json variable in javascript named jsonvar which is holding some json data. Now, to access the first key contained in parent json, then make use of array as shown above.

解释:假设在javascript中有一个名为jsonvar的json变量,它包含一些json数据。现在,要访问父json中包含的第一个键,请使用上面所示的数组。

Object.keys(jsonvar)[0]

种(jsonvar)[0]

will return the json key: "key". We are accessing the jsonvar to access the first key which corresponds to position [0].

将返回json键:“key”。我们正在访问jsonvar以访问与位置[0]对应的第一个键。

Hope this could solve your problem.

希望这能解决你的问题。