JSON Parsing in Angular - json

I am trying to get a list of ID from a JSON file.
So far, the only way to access the "id" object is by using this:
console.log(photos.photosets.photoset[0].id);
As you might tell, it only gives me the correct ID of the first item.
If I try this, it gives me an "undefined":
console.log(photos.photosets.photoset.id);

No Angular, just JavaScript.
for (i = 0; i < photos.photosets.length; i++) {
console.log(photos.photosets.photoset[i].id
}

You will need to iterate array of photoset's and produce new array of ids from it. Array.prototype.map is convenient in this case:
var ids = photos.photosets.photoset.map(function(obj) {
return obj.id;
});

Related

Node.js looping with json

I'm looking for the simplest way to loop throug a JSON file.
The Data Syntax (can't change that):
{"1":{"name":FOO","price":"1","sold":"100"},"2":{"name":"FOO","price":"1","sold":"100"}
The data is stored i a file called prices.json. How can I loop through all 7573 entrys?
Thanks..
You could simply require the json file then iterate over the properties of the object that it contains.
var prices = require('./prices.json');
for (var i in prices) {
if (prices.hasOwnProperty(i)) {
console.log(prices[i]); // do something with each item...
}
}

Manually parse json data according to kendo model

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.

How to format a json string and show result in table?

I am using CakePhp _serialize method to make a web service and show the data in JSON format.I used the .json extension at the end of the URL to show this data.Now i want to show this data in table.Output image is attached.Is this possible then how i can do it?
Thanks
The format is a bit odd. I would prefer something like: "task_list": [ .... ]; iterating over objects is always a bit tedious.
Here is the jQuery code:
var data = ...;
var items = data["task_list"];
var table = $('<table/>');
table.appendTo($('body'));
$.each(items, function(id, value) {
var tr = $('<tr/>');
table.append(tr);
$('<td/>').text(id).appendTo(tr);
$('<td/>').text(value).appendTo(tr);
});

Getting a specific value via index for a JSON object?

I've been trying to use a particular field of a JSON object.
When I use:
console.log(item);
Results:
[{"userid":"111","username":"","usertype":"0","emailaddress":"","phonenumber":"","faxnumber":"","cellnumber":"","gender":"5","race":"1","country":"1"}]
When I use:
console.log(item.userid);
Results:
undefined
You have to iterate over the array and then get the particular item
try using
for(var i=o;i<item.length;i++)
{
console.log(item[i].userid);
}

JSON - How to loop through a json object?

Im very new to JSON. I already tried the basics of JSON but in the examples Im using it is in array and the key is in string. Mine is quite different, its an object and the key is a number. Example of my JSON object is var ojbJSON = {55: 3, 23: 2};. My problems are I cannot get the length for an object because it doesn't have an attribute length and if I do something like alert(objJSON.55); to get the value 3 it causes a javascript error. Please don't ask why I'm insisting on this. Please help. Thanks.
You have to iterate to get the count:
var i=0;
for (var key in objJSON) i++;
alert(i);
You'd probably want to write a helper for this, something like:
function getLength(obj) {
var i=0;
for (var key in objJSON) i++;
return i;
}
alert(getLength(objJSON));
To get a key when it isn't a valid Javascript variable name (like numbers, or names that include hyphens, etc), you can use the bracket notation:
alert(objJSON[55]);