Javascript 从JSON中提取参数
我有一个JSON:Javascript 从JSON中提取参数,javascript,json,recursion,Javascript,Json,Recursion,我有一个JSON: {"id":"162","name":"Xiaomi Temperature Humidity Sensor","label":"Fridge Temperature","attributes":[{"name":"battery","currentValue":23,"
{"id":"162","name":"Xiaomi Temperature Humidity Sensor","label":"Fridge Temperature","attributes":[{"name":"battery","currentValue":23,"dataType":"NUMBER"},{"name":"batteryLastReplaced","currentValue":"Jun 19 2019","dataType":"STRING"},{"name":"humidity","currentValue":39.2,"dataType":"NUMBER"},{"name":"lastCheckinEpoch","currentValue":null,"dataType":"STRING"},{"name":"lastCheckinTime","currentValue":null,"dataType":"DATE"},{"name":"pressure","currentValue":100.14,"dataType":"NUMBER"},{"name":"temperature","currentValue":3.02,"dataType":"NUMBER"}],"capabilities":["TemperatureMeasurement",{"attributes":[{"name":"temperature","dataType":null}]},"RelativeHumidityMeasurement",{"attributes":[{"name":"humidity","dataType":null}]},"Battery",{"attributes":[{"name":"battery","dataType":null}]},"Sensor","PressureMeasurement",{"attributes":[{"name":"pressure","dataType":null}]}],"commands":["resetBatteryReplacedDate"]}
我试图提取温度(3.02)、压力(100.14)和湿度(39.2)。因为它是嵌套的(如果我理解正确的话?),我正在尝试我找到的一些递归代码。但我真的不知道如何提取我需要的数据
以下是我到目前为止的情况:
function updateTPH(deviceId, index, array) {
var responseString = '';
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
responseString = xhttp.responseText;
alert(responseString);
var element = document.getElementById(deviceId);
// Converting JSON object to JS object
var obj = JSON.parse(json);
// Define recursive function to print nested values
function printValues(obj) {
for(var k in obj) {
if(obj[k] instanceof Object) {
printValues(obj[k]);
} else {
document.write(obj[k] + "<br>");
};
}
};
// Printing all the values from the resulting object
printValues(obj);
}
};
xhttp.open("GET", "http://192.168.0.167/apps/api/" + myMakerAPI + "/devices/" + deviceId + "?access_token=" + myAccessToken, true);
xhttp.send();
};
函数updateph(设备ID、索引、数组){
var responseString='';
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
responseString=xhttp.responseText;
警觉(应变);;
var元素=document.getElementById(设备ID);
//将JSON对象转换为JS对象
var obj=JSON.parse(JSON);
//定义递归函数以打印嵌套值
函数打印值(obj){
用于(obj中的var k){
if(对象的obj[k]实例){
打印值(obj[k]);
}否则{
文件。写入(obj[k]+“
”;
};
}
};
//打印结果对象中的所有值
打印值(obj);
}
};
xhttp.open(“GET”http://192.168.0.167/apps/api/“+myMakerAPI+”/devices/“+deviceId+”?访问令牌=“+myAccessToken,true);
xhttp.send();
};
我正在将参数打印到页面上,但我无法解决如何将我想要的参数隔离到可以使用的变量中。任何帮助都将不胜感激 是,这些值是“嵌套的”。例如,如果要访问湿度,可以执行以下操作:obj[“attributes”][2][“currentValue”],因为湿度值位于“attributes”的第三个元素内
湿度:
obj[“属性”][2][“当前值”]代码>
压力:
obj[“属性”][5][“当前值”]代码>
温度:
obj[“属性”][6][“当前值”]代码>
您可以将这些值指定给变量,如下所示:
var-temperature=obj[“属性”][6][“当前值”]代码>是值是“嵌套的”。例如,如果要访问湿度,可以执行以下操作:obj[“attributes”][2][“currentValue”],因为湿度值位于“attributes”的第三个元素内
湿度:
obj[“属性”][2][“当前值”]代码>
压力:
obj[“属性”][5][“当前值”]代码>
温度:
obj[“属性”][6][“当前值”]代码>
您可以将这些值指定给变量,如下所示:
var-temperature=obj[“属性”][6][“当前值”]代码>您可以使用如下索引访问内部属性
myObj["attributes"][2]["currentValue"];
否则,您可以使用下面的代码生成如下所示的JSON对象,并使用myObj[“temperature”]
let myJSON={id:“162”,“名称”:“小米温湿度传感器”,“标签”:“冰箱温度”,“属性”:[{“名称”:“电池”,“当前值”:23,“数据类型”:“编号”},{“名称”:“电池放置”,“当前值”:“2019年6月19日”,“数据类型”:“字符串”},{“名称”:“湿度”,“当前值”:39.2,“数据类型”:“编号”},{“名称”:“lastCheckinEpoch”,“currentValue”:null,“数据类型”:“字符串”},{“名称”:“lastCheckinTime”,“currentValue”:null,“数据类型”:“日期”},{“名称”:“压力”,“currentValue”:100.14,“数据类型”:“数字”},{“名称”:“温度”,“currentValue”:3.02,“数据类型”:“数字”}],“能力”:[“温度测量”,{“属性”:[{“名称”:“温度”,“数据类型”}]相对湿度测量“,{”属性“:[{”名称“:”湿度“,”数据类型“:null}],”电池“,{”属性“:[{”名称“:”电池“,”数据类型“:null}],”传感器“,”压力测量“,{”属性“:[{”名称“:”压力“,”数据类型“:null}],”命令“:[“resetBatteryReplacedDate”];
让myAttributes=myJSON.attributes.reduce((对象,项)=>{
对象[item.name]=item.currentValue;
返回对象;
}, {});
log(myAttributes);
console.log(myAttributes.temperature);
您可以使用如下索引访问内部属性
myObj["attributes"][2]["currentValue"];
否则,您可以使用下面的代码生成如下所示的JSON对象,并使用myObj[“temperature”]
let myJSON={id:“162”,“name:”小米温湿度传感器”,“label:”冰箱温度”,“attributes:“{”name:”电池”,“currentValue:”23,“dataType:”NUMBER“},{”name:”batterylastreplace“,”currentValue:”2019年6月19日“,”dataType:”湿度“,”currentValue:”39.2,“dataType:”NUMBER“},{”name:”lastCheckinEpoch“,”currentValue:null,“数据类型”:“字符串”},{“名称”:“lastCheckinTime”,“currentValue:null,“数据类型”:“日期”},{“名称”:“压力”,“currentValue”:100.14,“数据类型”:“数字”},{“名称”:“温度”,“当前值”:3.02,“数据类型”:“数字”}],“能力”:[“温度测量”,{“属性”:[{“名称”:“温度”,“数据类型”:null}]相对湿度测量“,{”属性“:[{”名称“:”湿度“,”数据类型“:null}],”电池“,{”属性“:[{”名称“:”电池“,”数据类型“:null}],”传感器“,”压力测量“,{”属性“:[{”名称“:”压力“,”数据类型“:null}],”命令“:[“resetBatteryReplacedDate”];
让myAttributes=myJSON.attributes.reduce((对象,项)=>{
对象[item.name]=item.currentValue;
返回对象;
}, {});
log(myAttributes);
console.log(myAttributes.temperature);
不需要递归调用。您的数据类型已经预定义,所以您可以循环使用它
const obj={
身份证号码:“162”,
名称:“小米温湿度传感器”,
标签:“冰箱温度”,
属性:[
{名称:“电池”,当前值:23,数据类型:“数字”},
{
名称:“电池已放置”,
当前值:“2019年6月19日”,