parsing - how to parse json data to mootools javascript? -
im using mootools.
before getting json data
{"id":"120","name":"bassara","year":["1999","2003"],"cc":["2.4","2.5","3.0"],"type":"4","trans":["1"],"wd":["1","3"],"fuel":["1","2"],"hand":["1"],"hybrid":["1"]}
in javascript
function get_cdata(){ var jsonrequest = new request.json({ url: 'ajax_model_info.php?cid=' + cid, onsuccess: function(car){ (car.id.[1],car.name.[1],car.year.[1]) } }).send(); }
its ok. have data json
[{"7":{"1":0}},{"7":{"2":0}},{"7":{"3":0}},{"10":{"1":0}},{"10":{"2":0}},{"3":{"1":0}},{"3":{"2":0}},{"3":{"3":0}},{"3":{"4":0}},{"5":{"1":0}},{"5":{"2":0}},{"5":{"3":0}},{"5":{"4":0}}]
how data first example. because easy. sorry noob question.
this other array of objects need iterate.
var data = [{"7":{"1":0}},{"7":{"2":0}},{"7":{"3":0}},{"10":{"1":0}},{"10":{"2":0}},{"3":{"1":0}},{"3":{"2":0}},{"3":{"3":0}},{"3":{"4":0}},{"5":{"1":0}},{"5":{"2":0}},{"5":{"3":0}},{"5":{"4":0}}]; data.each(function(obj){ // mootools iterator object.each(obj, function(val, key){ console.log('key is: ' + key); // inner loop again or // or standard js iterator (var k in val){ if (val.hasownproperty(k)){ console.log(k, val[k]); } } }); });
mootools gives lots of tools working objects , arrays - read manual both types methods here: http://mootools.net/docs/core/types/object , http://mootools.net/docs/core/types/array
useful ones here may pluck, object.keys, object.values etc. many ways approach.
keep in mind numeric properties of objects not covered fifo in spec , chrome (webkit) tends sort them , produce them in wrong order.
Comments
Post a Comment