javascript - get specific value from json file by Id -
im using $.get json file content working fine,currently want filter , item id 4 (the last one) ,how should ? in jquery doc didn't find hint it... http://api.jquery.com/jquery.get/
this code:
$.get('tweets.json');
this json file content
[ { "id": 1, "tweet": "omg, worst day ever, bf @bobbyboo dumped me", "usersmentioned": [ { "id": 10, "username": "bobbyboo" } ] }, { "id": 2, "tweet": "omg, best day ever, bf came me" }, { "id": 3, "tweet": "omg, worst day ever, don't ask" }, { "id": 4, "tweet": "@bobbyboo omg...just omg!", "usersmentioned": [ { "id": 10, "username": "bobbyboo" } ] } ]
update
currenlty when try following dont anthing in then(function (tweets) ,tweets emtpy value entry4 filled correct value...
$.get('profile.json').then(function (profile) { $('#profile-pre').html(json.stringify(profile)); $.get('tweets.json', null, null, 'json') .then(function (response) { var entry4 = response.filter(function (tweet) { return tweet.id === 4; })[0]; return entry4 ; }) }).then(function (tweets) { $('#tweets-pre').html(json.stringify(tweets)); var myfriend = $.get('friend.json'); return myfriend }).then(function (friend) {
that be
$.get('tweets.json',null,null,'json') .then(function(response){ var iamnumberfour = response.filter(function(tweet){ return tweet.id === 4; })[0]; });
added type since if don't pass right headers, jquery won't parse json you.
$.get('profile.json').then(function(profile) { $('#profile-pre').html(json.stringify(profile)); return $.get('tweets.json', null, null, 'json') .then(function(response) { var entry4 = response.filter(function(tweet) { return tweet.id === 4; })[0]; return entry4; }) }).then(function(entry4) { $('#tweets-pre').html(json.stringify(tweets)); var myfriend = $.get('friend.json'); return myfriend })
Comments
Post a Comment