0

I need to run a function matchFound() if a string is found in an external json file.

This is what I have so far:

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!');   
}

But I don't know how to parse the Json data and search for a string.

I have this for XML (thanks @Ohgodwhy) but not sure how to translate for 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;   
            }
        }

    });
}

The search location within the variables is: responceData > feed > entries > [0] or 1 or [2] etc > contentSnippet

I only need to match the first 10 characters.

If a match is found then run the funciton matchFound() or if not found run the function matchNotFound()

Any help with this is very much appreciated.

C

enter image description here

1 Answer 1

1

you have to iterate json recursively and then search for the string

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
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks so much. Should var is_found = iterate(obj , string_to_find); be var is_found = iterate(data , string_to_find); ?
This is looking in title right? how do I look in responceData > feed > entries > [0] or [1] or [2] etc > contentSnippet
I need to loop through entries in the variable contentSnippet can that be done?
in that case you just have to remove the condition '"key === "title"' & write if(typeof(obj[key]) === "string" && obj[key].indexOf(string_to_find)!==-1)
Thanks... I always get 'No Match found!' even though the string is difinitely in json file. any ideas? I have exactly what your posted.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.