What I have..
$.getJSON('ui-DashboardWidgetsGet.php', function(msg)
{
alert(msg);
if(msg.error == "yes"){console.log('Error Found: '+ msg.errorMsg);}
else
{
ztsDashboardJSON = msg;
}
});
var ztsDashboardJSONCount = ztsDashboardJSON.widgets[0].length;
which dumps
{ "widgets": [{ "column1": [ {"weight": 1, "bID": 1, "hideMe": false, "collapse": false, "titleOf": "Test 1", "colorOf": "color-yellow", "theFunction": "functionName"}, {"weight": 2, "bID": 2, "hideMe": false, "collapse": false, "titleOf": "Test 2", "colorOf": "color-green", "theFunction": "functionName"}, {"weight": 3, "bID": 3, "hideMe": false, "collapse": false, "titleOf": "Test 3", "colorOf": "color-blue", "theFunction": "functionName"} ], "column2": [ {"weight": 1, "bID": 4, "hideMe": false, "collapse": false, "titleOf": "Test 4", "colorOf": "color-white", "theFunction": "functionName"}, {"weight": 2, "bID": 5, "hideMe": false, "collapse": false, "titleOf": "Test 5", "colorOf": "color-red", "theFunction": "functionName"}, {"weight": 3, "bID": 6, "hideMe": false, "collapse": false, "titleOf": "Test 6", "colorOf": "color-orange", "theFunction": "functionName"} ], "column3": [ {"weight": 1, "bID": 7, "hideMe": false, "collapse": false, "titleOf": "Test 7", "colorOf": "color-white", "theFunction": "functionName"}, {"weight": 2, "bID": 8, "hideMe": false, "collapse": false, "titleOf": "Test 8", "colorOf": "color-green", "theFunction": "functionName"}, {"weight": 3, "bID": 9, "hideMe": false, "collapse": false, "titleOf": "Test 9", "colorOf": "color-blue", "theFunction": "functionName"} ] }]}
Which is valid as per http://jsonlint.com/
It requests the php which echo’s that out from a db, it echo’s just that, nothing else. Yet I am trying to work with the result in the JavaScript there after and it doesn’t seem to be supplying me what I want..
my error:
ztsDashboardJSON.widgets is undefined [Break On This Error] var
ztsDashboardJSONCount = ztsDashboardJSON.widgets[0].length;
You need:
since “widgets” is a key mapped to an array of objects.
If you’re trying to get the length of a column, you can do:
Demo here.
To traverse your object to make use of all the widgets, columns and column values, you can do this:
Demo here.