Jquery search in json 1

Jquery search in json

$(function() {
    var json = {
        "people": {
            "person": [{
                "name": "Peter",
                "age": 43,
                "sex": "male"},
            {
                "name": "Zara",
                "age": 65,
                "sex": "female"}]
        }
    };
    $.each(json.people.person, function(i, v) {
        if (v.name.search(new RegExp(/peter/i)) != -1) {
            alert(v.age);
            return;
        }
    });
});

Here is what the above code is Doing:
1. We create a JSON object with two people.
2. We loop through the people and check if the name contains the string “peter”.
3. If it does, we alert the age of the person.

Similar Posts