0

我正在使用 jquery 创建一个自动完成组件,其中建议来自 Solr 的一个字段。我无法从相应的 json 格式中提取值。json 文件是给定的,我需要提取 Query 字段的值。

{
    "responseHeader": {
        "status": 0,
        "QTime": 0,
        "params": {
            "q": "Query:cr*\r\n",
            "wt": "json"
        }
    },
    "response": {
        "numFound": 16,
        "start": 0,
        "docs": [{
            "id": "90268125-f806-428c-889b-696172c55356",
            "Query": "cricket",
            "Hits": 66,
            "RespTime": 359},
        {
            "id": "d5871725-0d4f-42bc-8180-9328ed81c644",
            "Query": "cricketmania",
            "Hits": 66,
            "RespTime": 156}]
    }
}
4

1 回答 1

1
var data = {
    "responseHeader": {
        "status": 0,
        "QTime": 0,
        "params": {
            "q": "Query:cr*\r\n",
            "wt": "json"
        }
    },
    "response": {
        "numFound": 16,
        "start": 0,
        "docs": [{
            "id": "90268125-f806-428c-889b-696172c55356",
            "Query": "cricket",
            "Hits": 66,
            "RespTime": 359},
        {
            "id": "d5871725-0d4f-42bc-8180-9328ed81c644",
            "Query": "cricketmania",
            "Hits": 66,
            "RespTime": 156}]
    }
}

data.response.docs[0].Query

data.response.docs[1].Query

演示

使用循环:

$.each(data.response.docs, function(i, val) {
  console.log(val.Query);
});

演示

您还可以使用简单的 for 循环:

for( var key in data.response.docs ) {
   alert(data.response.docs[key].Query);
};

演示

于 2012-06-11T06:11:19.153 回答