从Luftdaten API特定传感器获取值

时间:2018-09-11 20:44:33

标签: javascript html api

新手问题。我想从以下位置获取当前传感器值:

var queryURL = "http://api.luftdaten.info/v1/sensor/13481/";
$.getJSON(queryURL, function(data) {
  var result = data.sensordatavalues
  console.log(result);
  var value = result.value
  $('#output').append('The value is ' + value);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>

我没有任何价值。怎么了?

1 个答案:

答案 0 :(得分:0)

您的api返回数组而不是对象。每个数组又具有单独的项数组。以下是来自API的示例响应。复制下面的JSON并将其放入JSON格式化工具中,看看它的外观

示例:https://jsonformatter.curiousconcept.com/

[{“ sampling_rate”:null,“ location”:{“ altitude”:“ 166.8”,“ longitude”:“ 15.7460”,“ latitude”:“ 60.3520”,“ country”:“ SE”,“ id “:6813},”传感器“:{” pin“:” 1“,” id“:13481,” sensor_type“:{”制造商“:” Nova Fitness“,”名称“:” SDS011“,” id“: 14}},“ id”:1896266747,“ sensordatavalues”:[{“ value_type”:“ P1”,“ id”:4046181511,“ value”:“ 1.67”},{“ value_type”:“ P2”,“ id “:4046181512,” value“:” 0.40“}],” timestamp“:” 2018-09-11 20:43:54“},{” sampling_rate“:null,” location“:{” altitude“:” 166.8 “,”经度“:” 15.7460“,”纬度“:” 60.3520“,”国家“:” SE“,” id“:6813},”传感器“:{” pin“:” 1“,” id“: 13481,“ sensor_type”:{“ manufacturer”:“ Nova Fitness”,“ name”:“ SDS011”,“ id”:14}},“ id”:1896277409,“ sensordatavalues”:[{“ value_type”:“ P1 “,” id“:4046203944,” value“:” 1.70“},{” value_type“:” P2“,” id“:4046203945,” value“:” 0.30“}],” timestamp“:” 2018-09 -11 20:46:22“}]