im using mootools.
before i getting this 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. but now i have to get data from this 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 to get data like first example. because its easy. sorry for noob question.
this is just like any other array of objects you need to 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
for (var k in val){
if (val.hasOwnProperty(k)){
console.log(k, val[k]);
}
}
});
});
Mootools gives you lots of tools for working with Objects and Arrays - read the manual for both Types methods here: http://mootools.net/docs/core/Types/Object and http://mootools.net/docs/core/Types/Array
Useful ones here may be pluck, Object.keys, Object.values etc. many ways to approach.
Keep in mind that numeric properties of objects are not covered by FIFO in the spec and Chrome (webkit) tends to sort them and produce them in the wrong order.
Related
Any built-in ready-to-use solution in Kendo UI to parse JSON data according to schema.model?
Maybe something like kendo.parseData(json, model), which will return array of objects?
I was searching for something like that and couldn't find anything built-in. However, using Model.set apparently uses each field's parse logic, so I ended up writing this function which works pretty good:
function parse(model, json) {
// I initialize the model with the json data as a quick fix since
// setting the id field doesn't seem to work.
var parsed = new model(json);
var fields = Object.keys(model.fields);
for (var i=0; i<fields.length; i++) {
parsed.set(fields[i], json[fields[i]]);
}
return parsed;
}
Where model is the kendo.data.Model definition (or simply datasource.schema.model), and json is the raw object. Using or modifying it to accept and return arrays shouldn't be too hard, but for my use case I only needed a single object to be parsed at a time.
I actually saw your post the day you posted it but did not have the answer. I just needed to solve this problem myself as part of a refactoring. My solution is for DataSources, not for models directly.
kendo.data.DataSource.prototype.parse = function (data) {
return this.reader.data(data);
// Note that the original data will be modified. If that is not what you want, change to the following commented line
// return this.reader.data($.extend({}, data));
}
// ...
someGrid.dataSource.parse(myData);
If you want to do it directly with a model, you will need to look at the DataReader class in kendo.data.js and use a similar logic. Unfortunately, the DataReader takes a schema instead of a model and the part dealing with the model is not extracted in it's own method.
I often work with large JavaScript objects and instead of manually opening and closing "branches", I would like to simply search for a particular string and show any key or value that matches.
Sort of like "grepping" for a keyword in a JavaScript object. Is this possible (especially in Chrome Dev Tool)?
Unfortunately I was hoping I could at least try the JSON.stringify() trick and then search on the raw JSON in a text editor, but I get the following error:
Uncaught TypeError: Converting circular structure to JSON
You can look at the object's keys and match against them:
function grepKeys(o, query){
var ret = {};
Object.keys(o).filter(function(key){
return key.includes(query);
}).forEach(function(key){ // can reduce instead
ret[key] = o[key]; // copy over
});
return ret;
}
Which'd let you return a partial object with all the keys that contain the string you specified. Note that this will not show any prototype keys but can be easily extended to allow it (by using a for... in instead of an Object.keys or by using recursion):
var o = grepKeys({buzz:5, fuzz:3, foo:4}, "zz");
o; // Object {buzz: 5, fuzz: 3}
Hi I'm new to Jquery and i don't know how to use json data. I have a Json which is coming as a response. My success function is
success: function(resp)
{
var x = JSON.stringify(resp);
alert(x);
}
alert is having following JSON
{"xyz":{"abc":[{"action":"fail","result":"xxx"},{"action":"pass","resut":"yyy"}]}}
I want action value alone. How can i get this value from x variable and how can i utilize this value in HTML. Thanks in advance.
When you use JSON.stringify() you are turning it into a string, you want it as an object to access the data. Try this:
success: function(resp) {
alert(resp.xyz.abc[0].action);
var x = resp.xyz.abc[0].action // to use the value in html later on
}
If it is returned as a string (I can't tell at this point), you can turn it into an object (as long as it is valid JSON) by using $.parseJSON()
success: function(resp)
{
var x = $.parseJSON(resp);
var xyz = x.xyz;
var pass = x.xyz.abc[1].action;
}
or you can loop though each of the array by $.each
success: function(resp)
{
var x = $.parseJSON(resp);
$.each(x.xyz.abc, function(index, element){
alert('action:' + element.action + ', result:' + element.resut)
});
}
i think, and don't take it personally,that your JSON object is not well organized as an object to get and to have. Action,from my perspective is either fail or success, and reading it, as you saw in the above good answer, will give you exactly what you want.
What's the point in getting a JSON with data structured like you did, with 2 possible answers encoded in it, when there is only one available (either success or fail).
this is my json format
({"message":{"success":true,"result":[{"lead_no":"LEA13","lastname":"Developer","firstname":"PHP","company":"Dummies","email":"nandhajj#gmail.com","id":"10x132"},{"lead_no":"LEA14","lastname":"Venu","firstname":"Yatagiri","company":"Rsalesarm","email":"veve#jajs.com","id":"10x133"},{"lead_no":"LEA4","lastname":"Jones","firstname":"Barbara","company":"Vtigercrm inc","email":"barbara_jones#company.com","id":"10x35"},{"lead_no":"LEA1","lastname":"Smith","firstname":"Mary","company":"Vtiger","email":"mary_smith#company.com","id":"10x32"}]}})
i am trying to retrieve the whole json result values using the following snippet
if (xmlHttp.readyState==4)
{
alert(xmlHttp.status);
if(xmlHttp.status==200)
{
alert("hi");
var jsondata=eval("("+xmlHttp.responseText+")") //retrieve result as an JavaScript object
jsonOutput=jsondata.result;
alert(jsonOutput);
InitializeLeadStorage()
}
}
my alert (hi ) is displayed but the alert(jsonOutput); is undefined , please help me if you could find any mistake
jsonOutput = jsondata.message.result;
result lives on message - it is not a top-level item in the JSON. With things like this, console.log() the JSON and you can check the path to the bit you want.
Also
your variable is global
there are better ways of parsing your JSON. If you don't care about old IEs, you can use the ECMA5 JSON.parse(), else use jQuery or another third-party utility for this
How do I display all the points in this Json return statement to an HTML table on my view?
return Json(
new
{
vol = exposure.Volatility,
points =
from point in exposure.ExposurePointCollection
select new
{
date = point.ExposureDate,
val = point.MaximumExposure
}
});
If this is the result of an ajax call, you will need to iterate over the collection (presumably the "points" value) and add them to the DOM via javascript. jQuery can help with this, but there are also plug-ins for jQuery that will do virtually all of the work for you, something like jqGrid comes to mind.
Looping through the json result (assuming "points" is what you are looping over)
success: function(data){
jquery.each(data.points, function(index, value){
//index is the index of the array, value is the actual value
});
}
Look here for using jQuery to modify the DOM: http://api.jquery.com/html/#html2