0

我从 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 对象中的“功能”属性构建一个表。如何在 javascript 中访问“功能”属性(它是一个子 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);
                                    };

                                }
                            });

谢谢!!!

4

2 回答 2

2

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

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

如果它不是一个对象,你会做JSON.parse(result)

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

于 2013-10-21T14:44:25.603 回答
1

JSON 创建普通的 Javascript 对象。

您可以像访问任何其他对象一样访问它们的属性:

var myValue = myObject.features.x.visual_type;
于 2013-10-21T14:43:48.973 回答