如何打印json对象属性值?

时间:2016-11-06 00:46:51

标签: javascript json

我有一个像这样的json对象:

JsonQuery = ​'{
    "from": 0,
    "size": 200,
    "sort": [{
        "Modified": {
            "order": "desc"
        }
    }],
    "query": {
        "bool": {
            "must": [{
                "term": {
                    "CollectionId": {
                        "value": "abcd"
                    }
                }
            }, {
                "terms": {
                    "Container": ["en-us"]
                }
            }],
            "must_not": [{
                "wildcard": {
                    "_type": {
                        "value": "@@"
                    }
                }
            }, {
                "bool": {
                    "filter": {
                        "exists": {
                            "field": "DynamicProperties.MainSpec"
                        }
                    },
                    "filter": {
                        "exists": {
                            "field": "DynamicProperties.ExtendedSpec"
                        }
                    }
                }
            }]
        }
    }
}';

我正在通过

创建一个javascript对象
var obj = JSON.parse(JsonQuery);

我进入chrome控制台并编写obj并按Enter键并正确显示该对象但当我尝试访问该对象的属性时,它仍然说未定义。 例如:我正在使用obj.size

1 个答案:

答案 0 :(得分:1)

你的第一行只有一个gremlin(JsonQuery ='),尝试删除它并重试。

jsonQuery = '{"from":0,"size":200,"sort":[{"Modified":{"order":"desc"}}],"query":{"bool":{"must":[{"term":{"CollectionId":{"value":"abcd"}}},{"terms":{"Container":["en-us"]}}],"must_not":[{"wildcard":{"_type":{"value":"@@"}}},{"bool":{"filter":{"exists":{"field":"DynamicProperties.MainSpec"}},"filter":{"exists":{"field":"DynamicProperties.ExtendedSpec"}}}}]}}}';

var obj = JSON.parse(jsonQuery);

console.log(obj.size);