0

I am using the following function as a test case for my application:

$(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 == "Peter") {
        alert(v.age);
        return;
    }
});
});

This simply searches a set of JSON data for a static input, which in this case is the name "Peter", and returns his 'age'. I want to modify this function to pull input from a text field and use it in place of the static string "Peter" so I can search for any name Id like. The function will be called from a submit button.

That is my first obstacle.

My second is Id like to use the subset of the result as an object, rather than a string, in my application. In other words, if I placed a name in a text field the function would find the name within the JSON data and return, in this case, 'age' as an object that I can then use for other calculations in the work flow.

Can anyone help me with this?

Thanks!

2 Answers 2

1

i made a fiddle that makes what you need http://jsfiddle.net/KmYJw/1/

lets say you have 2 form fields . 1 text & 1 submit

<input name="name" value="" type="text" />
<input value="search in json" type="submit" />

when you click submit . you need to take the value from the text input and search for in in the json

    var age; // global var

    $("input[type='submit']").click(function() {
        var searchName = $("input[name='name']").val(); // take value of text input 

        $.each(json.people.person, function(i, v) {
            if (v.name == searchName) {
                age = v.age; // attach search age to global var
                return;
            }
        });
        return;
    });
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks so much for laying this out. ?so, in this esample, I should be able to now use 'age' in calculations just as I would any other varibale that was previously defined, correct?
Thats perfect; thanks! Would it possible to have the results of the search displayed in a list? For example: If the text field searched for a specific zip code and the results returned 10 business names instead of just one? Users would then need to select a specific business name and have all of the related items returned as object.Thanks again for your help!
The main thing Im asking is what if there were more than one result returned from the search box? For example: if I entered 'Peter' in the text field and there were multiple people named 'Peter' in the JSON file, but of differing ages, how could I separate the different objects?
1

If your input field is called name you can use the following:

var name = $("input[name='name']").val();
$.each(json.people.person, function(i, v){
if(v.name == name){
...

As for your second requirement, you can just return the v variable instead of returning nothing and alerting the result.

2 Comments

Thanks for your comment. So would the second second look like such:
Sorry I'm not seeing anything. Can you post again?

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.