Answers for "Jquery search in json - Get json data"

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;
        }
    });
});
Posted by: Guest on May-09-2021
0

Jquery search in json - Get json data

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.getJSON("json1.json", function(result){
            $.each(result, function(i, obj){
                $("div").append(obj.first + " " + obj.last + " " + obj.desc + " " + "<br>");
            });
        });
    });
});
</script>
</head>
<body>

<button>Get JSON data</button>

<div></div>

</body>
</html>
Posted by: Guest on May-09-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language