Reputation: 193
I'm using jQuery to read a JSON file containing a simple array of numbers, as such:
({
"values": [
{
"12",
"32",
"21",
"23",
"34"
"43",
"52",
"86",
"25"
}
]})
My JS (as follows) keeps returning "undefined". Can someone please help?
$('#dataviewer').live('pageshow', function () {
$.getJSON("test.json", "chartData");
$(function chartData(data) {
var chartString = "";
$.each(data, function(index){
chartString += index.values;
});
alert('chartString' + chartString);
});
});
Upvotes: 0
Views: 325
Reputation: 95056
There's a lot wrong with your javascript, it should be more like this:
$('#dataviewer').live('pageshow', function () {
$.getJSON("test.json", chartData);
function chartData(data) {
var chartString = "";
$.each(data.values, function(index,val){
chartString += val;
});
// or simply...
// var chartString = data.values.join(",");
alert('chartString' + chartString);
}
});
Also, json is obviously invalid, as has been stated in many other answers.
Upvotes: 0
Reputation: 13763
It should be
{
"values": [
"12",
"32",
"21",
"23",
"34",
"43",
"52",
"86",
"25"
]
}
Upvotes: 2
Reputation: 2812
try removing '{' and '}'
({"values": [ "12", "32", "21", "23", "34", "43", "52", "86", "25"]})
Upvotes: 0
Reputation: 5329
I think it should be:
{ "values": [
"12",
"32",
"21",
"23",
"34"
"43",
"52",
"86",
"25" ]}
Upvotes: 1