gpt4 book ai didi

javascript - 在javascript中解析复杂的json

转载 作者:行者123 更新时间:2023-11-28 20:13:49 24 4
gpt4 key购买 nike

我从 Mongo DB 获取 JSON 对象。这是 JSON。

**JSON**
{
"_id" : ObjectId("5265347d144bed4968a9629c"),
"name" : "ttt",
"features" : {
"t" : {
"visual_feature" : "t",
"type_feature" : "Numeric",
"description_feature" : "Time"
},
"y" : {
"visual_feature" : "y",
"type_feature" : "Nominal",
"description_feature" : "Values to be mapped to the y-axis"
},
"x" : {
"visual_feature" : "x",
"type_feature" : "Numeric",
"description_feature" : "Values to be mapped to the x-axis"
}
}
}

我正在尝试根据 JSON 对象中的“features”属性构建一个表。如何在javascript中访问“features”属性(它是一个子json对象)?从“visual_feature”、“type_feature”和“description_feature”中获取值非常重要。UPD我有一个解决方案。

  $.ajax({
url: VASERVER_API_LOC + '/visualization/' + visid + '/',
type: 'GET',
contentType: "application/json",
data: tmp_object,
success: function(json) {
var result = [];
var keys = Object.keys(json);
keys.forEach(function (key){
result.push(json[key]);
});

for(var i=0; i<result.length; i++){
console.log(">>> visual_feature == " + result[i].visual_feature);
console.log(">>> type_feature == " + result[i].type_feature);
console.log(">>> discription_feature == " + result[i].description_feature);
};

}
});

谢谢!!!

最佳答案

假设您的 JSON 结果是一个对象,请像这样循环:

for (var feature in result.features) {
if (object.hasOwnProperty(feature)) {
// do table building stuff
console.log(feature);
}
}

如果它不是一个对象,您将执行JSON.parse(result)

要访问子属性,您可以在内部执行另一个 for in 循环。

关于javascript - 在javascript中解析复杂的json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19497515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com