2016-11-21 68 views
0

我使用jQuery中的AJAX調用從API中檢索JSON對象。這是我收到回一個例子對象:如何使用jQuery在對象內搜索特定字符串的JSON對象

... 
    "batchcomplete": "", 
    "query": { 
    "normalized": [ 
     { 
     "from": "star trek", 
     "to": "Star trek" 
     } 
    ], 
    "pages": { 
     "1048985": { 
     "pageid": 1048985, 
     "ns": 0, 
     "title": "Star trek", 
     "extract": "<ul><li><b>From other capitalisation</b>: This is a redirect from a title with another method of capitalisation. It leads to the title in accordance with the Wikipedia naming conventions for capitalisation, or it leads to a title that is associated in some way with the conventional capitalisation of this redirect title. This may help writing, searching and international language issues.\n<ul><li>If this redirect is an incorrect capitalisation, then {{R from miscapitalisation}} should be used <i>instead</i>, and pages that use this link should be updated to link <i>directly</i> to the target. Miscapitisations can be tagged in <i>any namespace</i>.</li>\n<li>Use this rcat to tag <i>only</i> mainspace redirects; when other capitalisations are in other namespaces, use {{R from modification}} <i>instead</i>.</li>\n</ul></li>\n</ul>" 
     } 
    } 
    } 
} 

我還有一個JSON對象回來,看起來像這樣:

{ 
    "batchcomplete": "", 
    "query": { 
    "normalized": [ 
     { 
     "from": "set", 
     "to": "Set" 
     } 
    ], 
    "pages": { 
     "454886": { 
     "pageid": 454886, 
     "ns": 0, 
     "title": "Set", 
     "extract": "<p><b>Set</b> or <b>The Set</b> may refer to:</p>\n\n" 
     } 
    } 
    } 
} 

什麼,我試圖做的是尋找這些對象的特定字符串以便對結果執行邏輯以檢索我實際正在查找的數據。在第一個例子中,我想在「extract」值中找到'From other capitalization',而在第二個例子中,我想找到'可能指:'。

有沒有辦法爲特定的字符串搜索特定的JSON對象並返回該字符串是否存在?

+0

所以,你每次需要不同的字符串? – br3t

+0

不是真的。這是我需要搜索的唯一兩個字符串。這兩個字符串都會觸發結果的不同邏輯。 –

回答

1

針對您的特殊情況下,我會做這樣的事情

function searchResponse(response, query){ 

    // loop through the objects keys which appear to be different page numbers 
    for (const pageNum in response.pages){ 
    // reference the actual page object 
    const page = response.pages[pageNum]; 


    // test the extract against the query.   
    if (page.extract.indexOf(query) !== -1) { 
     return true; 
    } 
    } 
    return false; 
} 

然後你會調用與任何搜索詞您正在尋找每個響應的功能。 true的結果意味着響應包含查詢。但是,如果api返回多個頁面,則這不起作用,因爲它會搜索每個頁面,並且return true如果任何頁面包含query,這可能不是所需的結果。

+0

謝謝。我不知道爲什麼indexOf沒有來找我解決這個問題。 –

0

我希望這可以幫助你。使用普通的javascript遍歷鍵並找到所需的對象。

var obj = { 
 
    "batchcomplete": "", 
 
    "query": { 
 
    "normalized": [{ 
 
     "from": "set", 
 
     "to": "Set" 
 
    }], 
 
    "pages": { 
 
     "454886": { 
 
     "pageid": 454886, 
 
     "ns": 0, 
 
     "title": "Set", 
 
     "extract": "<p><b>Set</b> or <b>The Set</b> may refer to:</p>\n\n" 
 
     } 
 
    } 
 
    } 
 
}; 
 

 
var searchStr = " may refer to"; 
 
var results = []; 
 
Object.keys(obj.query.pages).forEach(function(key) { 
 
    if (obj.query.pages[key].extract.includes(searchStr)) { 
 
    results.push(obj.query.pages[key].extract); 
 
    } 
 
}); 
 

 
results.forEach(function(res){ console.log(res); });

+0

我沒有考慮包含作爲一個選項。它只適用於在jQuery中測試字符串,還是可以用於測試其他數據類型? –

+0

'includes'適用於任何'String',但它是ES2015,可能還不支持所有瀏覽器。 –

+0

@AustinEzell你可以使用polyfill。但只是爲了這個任務使用jquery將是矯枉過正我猜。 – WitVault