doulu6234 2015-05-18 08:13
浏览 53
已采纳

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

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.

Exemple of my 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].

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].

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 ?

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

EDIT : here is the code I am using in 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条回答 默认 最新

  • dougu2006 2015-05-18 08:29
    关注

    You can access with the Object.keys to the name of the property in 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):

    With PHP is the same but using 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

    Hope help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c