Sunday, 15 June 2014

javascript - how to parse json array -



javascript - how to parse json array -

i'm trying parse this

{ "popular result":[ {"term":"summer","url":"http://summer.com"}, {"term":"autumn","url":"http://autumn.com"}, {"term":"spring","url":"http://spring.com/"}, {"term":"winter","url":"http://winter.com/"}] } <script type="text/javascript"> $(document).ready(function () { $.getjson('/controls/getpopularsearches', function (json) { (var = 0; < json.length; i++) { $.each(myjson.i, function (key, value) { alert(value.term); });​ } }); }); </script>

but nil happened! because array in array! please allow me know how this

arrays , objects different things. want investigate them tons more before things really challenging.

assuming json equal object provide (in json show {}), json['popular result'] (you utilize . if there wasn't space) array (in json show @ []) want traverse.

for reason, confusion got looping on object (not going anywhere length defined it) , (ignoring typo on myjson), started looping on didn't exist (which didn't crash b/c never got there).

cleaning up...

<script type="text/javascript"> $(document).ready(function () { $.getjson('/controls/getpopularsearches', function (json) { (var i=0;i<json['popular result'].length;i++) { alert(json['popular result'][i].term + ' points url ' + json['popular result'][i].url); } }); }); </script>

notice how alert references json object (that's variable name), popular result array, [i] "row" in array, , term/url element of object on row.

note: running ton of alerts you're debugging annoying. check out console.log.

javascript json

No comments:

Post a Comment