Javascript/jQuery在json中搜索字符串,然后运行函数
如果在外部json文件中找到字符串,我需要运行函数matchFound() 这就是我到目前为止所做的:Javascript/jQuery在json中搜索字符串,然后运行函数,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,如果在外部json文件中找到字符串,我需要运行函数matchFound() 这就是我到目前为止所做的: function init(){ $.ajax({ type: "GET", url: "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=50&callback=?&q=http://www.domain.com/feed.rss", dataType:
function init(){
$.ajax({
type: "GET",
url: "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=50&callback=?&q=http://www.domain.com/feed.rss",
dataType: "json",
success: parseData
});
}
function parseData(data){
var string_to_find = "Hello World!";
// look into the data and match the string
}
function matchFound(str, string_to_find){
alert('Match found in string - '+ str +'\r\n While looking for the string- '+ string_to_find);
return true;
}
function matchNotFound(){
alert('No Match found!');
}
但我不知道如何解析Json数据和搜索字符串
我有这个XML(谢谢@Ohgodwhy),但不知道如何翻译为json
function parseXml(xml){
var string_to_find = "Hello World!";
$(xml).find('title').each(function(){
var str = $(this).text();
if(str.indexOf(string_to_find) > -1){
if(matchFound(str, string_to_find)){
return false;
}
}
});
}
变量中的搜索位置是:responceData>feed>entries>[0]或[2]etc>contentSnippet
我只需要匹配前10个字符
如果找到匹配项,则运行函数matchFound(),如果未找到,则运行函数matchNotFound()
非常感谢您的帮助
C
必须递归迭代json,然后搜索字符串
function parseData(data){
var string_to_find = "Hello World!";
var is_found = iterate(data , string_to_find);
if(is_found === true){
matchFound(JSON.stringify(data), string_to_find);
}else{
matchNotFound(string_to_find);
}
// look into the data and match the string
}
function iterate(obj , string_to_find) {
for(var key in obj) { // iterate, `key` is the property key
var elem = obj[key]; // `obj[key]` is the value
console.log(elem, string_to_find);
if(typeof(elem)=="string" && elem.indexOf(string_to_find)!==-1) {
return true;
}
if(typeof elem === "object") { // is an object (plain object or array),
// so contains children
return iterate(elem , string_to_find); // call recursively
}
}
}
非常感谢。应该
var is\u found=iterate(obj,string\u to\u find)
bevar is\u found=iterate(数据、字符串到\u found)代码>?这是在标题中查找的,对吗?如何查看responceData>feed>entries>[0]或[1]或[2]etc>contentSnippet
我需要循环查看变量contentSnippet
中的entries
这可以做到吗?在这种情况下,您只需删除条件“key==”title“&write if(typeof(obj[key])==”string“&obj[key]”indexOf(string_to_find)!==-1)谢谢…我总是得到“找不到匹配项!”
,即使字符串确实在json文件中。有什么想法吗?我完全知道你发布的内容。