2016-11-21 60 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