如何从嵌套的JSON字符串中的数组中检索值?

时间:2012-09-12 15:46:15

标签: jquery json

如何从此JSON字符串中的数组paramList(在name":"offset","values":["90"]开头)中检索值90?

{"query":{"query":"skole","hits":10,"paramList":[{"name":"hits","values":["10"]},{"name":"offset","values":["90"]},{"name":"q","values":["skole"]}]},"status":"success","facets":[{"selectionType":"AND","numberOfItems":1,"selectableItems":[{"value":"Intranett","displayName":"Intranett","count":146,"paramId":"?hits=10&q=skole&kilde=Intranett"}],"appliedItems":[],"facetName":"Kilde"},{"selectionType":"AND","numberOfItems":4,"selectableItems":[{"value":"EPiServerPage","displayName":"EPiServerPage","count":90,"paramId":"?hits=10&q=skole&type_s=EPiServerPage"},{"value":"Word","displayName":"Word","count":51,"paramId":"?hits=10&q=skole&type_s=Word"},{"value":"ClubNews","displayName":"ClubNews","count":4,"paramId":"?hits=10&q=skole&type_s=ClubNews"},{"value":"Project","displayName":"Project","count":1,"paramId":"?hits=10&q=skole&type_s=Project"}],"appliedItems":[],"facetName":"Type"}],"suggestions":{"displayName":"spellingSuggestion","suggestions":null,"collatedSuggestion":{"suggestion":null,"paramId":"?q=null"}},"errors":[],"numberOfHits":146,"pagination":{"offset":90,"first":{"displayName":"1","selected":false,"end":10,"start":1,"paramId":"?hits=10&q=skole&offset=1"},"next":{"displayName":"11","selected":false,"end":109,"start":100,"paramId":"?hits=10&q=skole&offset=100"},"hitsPerPage":10,"paginationItems":[{"displayName":"5","selected":false,"end":49,"start":40,"paramId":"?hits=10&q=skole&offset=40"},{"displayName":"6","selected":false,"end":59,"start":50,"paramId":"?hits=10&q=skole&offset=50"}, .....

为了检索元素查询(第一行的开头),我使用{$T.query.query}

4 个答案:

答案 0 :(得分:1)

$T.query.query.paramList[1].values[0]假设结构不会改变

答案 1 :(得分:0)

var val = jsonArray["query"]["paramList"][1]["values"];

var q = jsonArray["query"]["paramList"];

var values = q.map(function(){

    return values[0];

});

var index = values.indexOf(90);

答案 2 :(得分:0)

如果您只查找该特定值,并且知道它在JSON中的位置,您可以使用:

var json = 'YOUR JSON STRING HERE';
var obj = $.parseJSON(json);
var value = obj.query.paramList[1].values[0];

答案 3 :(得分:-1)

评估您的JSON

JSON.query.paramList[1].values.[0];

返回90。