Squishy
Squishy

Reputation: 193

Reading JSON in JQuery Mobile returning "undefined"

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

Answers (4)

Kevin B
Kevin B

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

Govind Malviya
Govind Malviya

Reputation: 13763

It should be

{
    "values": [
        "12",
        "32",
        "21",
        "23",
        "34",
        "43",
        "52",
        "86",
        "25"
    ]
}

Upvotes: 2

chepe263
chepe263

Reputation: 2812

try removing '{' and '}'

({"values": [ "12",  "32", "21", "23", "34", "43", "52", "86", "25"]})

Upvotes: 0

dagnelies
dagnelies

Reputation: 5329

I think it should be:

   { "values": [
         "12",
         "32",
         "21",
         "23",
         "34"
         "43",
         "52",
         "86",
         "25" ]}

Upvotes: 1

Related Questions