【问题标题】:How to access JSON object without knowing its name (and that is not in an array)如何在不知道其名称的情况下访问 JSON 对象(并且不在数组中)
【发布时间】:2015-07-29 15:47:15
【问题描述】:

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

我的 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>";
}

【问题讨论】:

  • 为什么你不使用 JSON.parse(json)

标签: php arrays json object getjson


【解决方案1】:
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]

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

希望这可以解决您的问题。

【讨论】:

  • 你能详细说明你的答案吗?我不明白这将如何解决我的问题。也许您可以向我展示您对我的问题的解决方案的实施?如果你知道 PHP 的实现,那就更好了。
  • @PiggyGenius,我已经编辑/更新了答案,相关解释是支持。我已经发布了 javascript 的代码。
【解决方案2】:

您可以使用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 相同,但使用 keystdClassjson_decode 返回 stdClass

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

See demo

希望帮助。

【讨论】:

  • 您的解决方案会产生错误:“不能将 stdClass 类型的对象用作数组”。我要编辑问题,我会添加我的代码,也许我在其他地方做错了,它会帮助你。
  • 因为我不知道你想要在 PHP 中。我已经编辑了我的答案。请参阅 PHP 版本。
【解决方案3】:

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

【讨论】:

  • 非常感谢您花时间回答如此详细的解决方案。我在你之前尝试了 Pedro Gamez 的解决方案,它正在工作。但也许我以后需要使用您的解决方案,无论如何非常感谢您
猜你喜欢
  • 2016-07-02
  • 1970-01-01
  • 2011-07-04
  • 2021-01-27
  • 1970-01-01
  • 2010-10-30
  • 2013-05-10
  • 2021-09-25
  • 1970-01-01
相关资源
最近更新 更多