Solved:
$.getJSON('contacts.json', function (json) {
var array = [];
for (var key in json) {
if (json.hasOwnProperty(key)) {
var item = json[key];
array.push({
name: item.Name,
surname: item.Surname,
mobile: item.mobile,
email: item.email
});
}
}
});
Answer from Kakitori on Stack OverflowVideos
Solved:
$.getJSON('contacts.json', function (json) {
var array = [];
for (var key in json) {
if (json.hasOwnProperty(key)) {
var item = json[key];
array.push({
name: item.Name,
surname: item.Surname,
mobile: item.mobile,
email: item.email
});
}
}
});
var items = [];
$.each(JSONObject.results.bindings, function(i, obj) {
items.push([obj.place.value, obj.lat.value, obj.long.value, obj.page.value]);
});
As long as the file is inside your project folder (config file, i.e.) you can load it synchronously requiring it directly in NodeJS.
var test = require('./array.json');
And then the content will be loaded into your variable in the next executed sentence.
You can try to console.log it, and it will print:
[ { name: 'c', content: '3', _prototype: 'item' },
{ name: 'd', content: '4', _prototype: 'item' } ]
Right exactly in the order that the file had.
fs.stat is async, so your function is async.
You want fs.fstatSync instead.
You seem to have the order wrong, data is the array so it's not
data.name[i]
but
data[i].name
and removing the strange apply it would be
$.getJSON('json.js',function (data) {
for (var i = 0; i < data.length; i++) {
name.push( data[i].name );
address.push( data[i].address );
city.push( data[i].city );
}
});
Also note that name is a bad choice for a variable name in the global context.
You have two or three problems.
Your JSON is invalid.
Remove the quotes from around the outside of it.
Seriously consider giving is a .json file extension, then you are more likely to get the right MIME type for JSON. (JSON data files are not JavaScript programs).
File Json : "data.json"
[
{"name ": "John", "address": "350 Fifth Avenue", "city ": "New York"},
{"name ": "Mark", "address": "1101 Arch St", "city ": "Philadelphia"},
{"name ": "Jack", "address": "60th Street", "city ": "Chicago"}
]
You are accessing your data in the wrong order
This is overly complicated and is accessing properties in the wrong order.
name.push.apply(name, data.name[i]);
Forget about using apply. You don't need it.
Then note that your JSON array contains the objects, not the other way around. You need to access the array index before the property name:
name.push(data[i].name);
Remember the A in Ajax
Finally - your code doesn't show how you are testing the result. Remember that Ajax is asynchronous, so that if you examine the values of your arrays outside the callback function, they might not have been populated yet.
Javascript has a built in JSON parse for strings, which I think is what you have:
var myObject = JSON.parse("my json string");
to use this with your example would be:
var jsonData = JSON.parse(myMessage);
for (var i = 0; i < jsonData.counters.length; i++) {
var counter = jsonData.counters[i];
console.log(counter.counter_name);
}
Here is a working example
EDIT: There is a mistake in your use of for loop (I missed this on my first read, credit to @Evert for the spot). using a for-in loop will set the var to be the property name of the current loop, not the actual data. See my updated loop above for correct usage
IMPORTANT: the JSON.parse method wont work in old old browsers - so if you plan to make your website available through some sort of time bending internet connection, this could be a problem! If you really are interested though, here is a support chart (which ticks all my boxes).
In a for-in-loop the running variable holds the property name, not the property value.
for (var counter in jsonData.counters) {
console.log(jsonData.counters[counter].counter_name);
}
But as counters is an Array, you have to use a normal for-loop:
for (var i=0; i<jsonData.counters.length; i++) {
var counter = jsonData.counters[i];
console.log(counter.counter_name);
}