2012-09-12 50 views
1

如何從這個JSON字符串中的數組paramList(在name":"offset","values":["90"]的開頭)檢索值90?如何從嵌套的JSON字符串中的數組中檢索值?

{"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}

回答

1

$T.query.query.paramList[1].values[0]假設該結構不會改變

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); 
+0

'values'是一個數組,而這正是這個表達式會。 –

+1

也,即使這*是正確的,它將返回10,而不是90 –

+0

@PhillipSchmidt:對不起,更正和更新。感謝您的高舉。 –

0

如果您只查看特定值並知道它將在哪E在JSON,你可以使用:

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

你的JSON評估

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

回報90.

+0

嗯......這看起來很像我的答案。除了我的答案有正確的好處。 –

+0

沒有花太多時間回答這個問題,而且我在提交答案後已經強化了。但是,有第二個查詢。 – Stefan

相關問題