jquery - Parsing JSON Data/Converting to Objects -
i using following function test case 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 searches set of json data static input, in case name "peter", , returns 'age'. want modify function pull input text field , use in place of static string "peter" can search name id like. function called submit button.
that first obstacle.
my second id use subset of result object, rather string, in application. in other words, if placed name in text field function find name within json data , return, in case, 'age' object can use other calculations in work flow.
can me this?
thanks!
i made fiddle makes need http://jsfiddle.net/kmyjw/1/
lets have 2 form fields . 1 text & 1 submit
<input name="name" value="" type="text" /> <input value="search in json" type="submit" />
when click submit . need take value text input , search in in 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 global var return; } }); return; });
Comments
Post a Comment