JSON error - Doesnt support valid JSON?

JSON error - Doesnt support valid JSON?

jagdipajagdipa Posts: 14Questions: 0Answers: 0
edited August 2011 in General
I have the following database and code that I am trying to bind. I have just copied the Javascript Array example to try to get this to work. But I get the error
'DataTables warning (tableid = 'example': Requested unknown parameter '0' from data source for row 0.'

What am I doing wrong?

[code]
var test = [{ "ID": "1", "parentColumnSortID": "0", "description": "Precondition", "columnSortID": "1", "itemType": 0, "agent": null, "warningsAndCautions": null, "notes": null, "potentialHumanFailures": null, "errorDescription": null, "potentialConsequences": null, "existingRiskControlMeasures": null, "performanceInfluencingFactors": null, "riskReductionMeasures": null, "pIFStatement": null, "pIFAssessment": null, "comments": null, "severityOfConsequences": null, "likelihoodOfError": null, "likelihoodOfRecovery": null, "rankScore": null, "logic": null, "qi": null, "hEPAProbability": null, "alternateProbability": null, "activity": null },
{ "ID": "2", "parentColumnSortID": "1", "description": "Goals", "columnSortID": "2", "itemType": 1, "agent": null, "warningsAndCautions": null, "notes": null, "potentialHumanFailures": null, "errorDescription": null, "potentialConsequences": null, "existingRiskControlMeasures": null, "performanceInfluencingFactors": null, "riskReductionMeasures": null, "pIFStatement": null, "pIFAssessment": null, "comments": null, "severityOfConsequences": null, "likelihoodOfError": null, "likelihoodOfRecovery": null, "rankScore": null, "logic": null, "qi": null, "hEPAProbability": null, "alternateProbability": null, "activity": null },
{ "ID": "3", "parentColumnSortID": "2", "description": "Do in sequence", "columnSortID": "3", "itemType": 2, "agent": null, "warningsAndCautions": null, "notes": null, "potentialHumanFailures": null, "errorDescription": null, "potentialConsequences": null, "existingRiskControlMeasures": null, "performanceInfluencingFactors": null, "riskReductionMeasures": null, "pIFStatement": null, "pIFAssessment": null, "comments": null, "severityOfConsequences": null, "likelihoodOfError": null, "likelihoodOfRecovery": null, "rankScore": null, "logic": null, "qi": null, "hEPAProbability": null, "alternateProbability": null, "activity": null },
{ "ID": "4", "parentColumnSortID": "3", "description": "", "columnSortID": "3.1", "itemType": 3, "agent": null, "warningsAndCautions": null, "notes": null, "potentialHumanFailures": null, "errorDescription": null, "potentialConsequences": null, "existingRiskControlMeasures": null, "performanceInfluencingFactors": null, "riskReductionMeasures": null, "pIFStatement": null, "pIFAssessment": null, "comments": null, "severityOfConsequences": null, "likelihoodOfError": null, "likelihoodOfRecovery": null, "rankScore": null, "logic": null, "qi": null, "hEPAProbability": null, "alternateProbability": null, "activity": null },
{ "ID": "unit2", "parentColumnSortID": "3", "description": "", "columnSortID": "3.2", "itemType": 3, "agent": null, "warningsAndCautions": null, "notes": null, "potentialHumanFailures": null, "errorDescription": null, "potentialConsequences": null, "existingRiskControlMeasures": null, "performanceInfluencingFactors": null, "riskReductionMeasures": null, "pIFStatement": null, "pIFAssessment": null, "comments": null, "severityOfConsequences": null, "likelihoodOfError": null, "likelihoodOfRecovery": null, "rankScore": null, "logic": null, "qi": null, "hEPAProbability": null, "alternateProbability": null, "activity": null },
{ "ID": "unit3", "parentColumnSortID": "3", "description": "", "columnSortID": "3.3", "itemType": 3, "agent": null, "warningsAndCautions": null, "notes": null, "potentialHumanFailures": null, "errorDescription": null, "potentialConsequences": null, "existingRiskControlMeasures": null, "performanceInfluencingFactors": null, "riskReductionMeasures": null, "pIFStatement": null, "pIFAssessment": null, "comments": null, "severityOfConsequences": null, "likelihoodOfError": null, "likelihoodOfRecovery": null, "rankScore": null, "logic": null, "qi": null, "hEPAProbability": null, "alternateProbability": null, "activity": null}];

$(document).ready(function () {
$('#dynamic').html('');
$('#example').dataTable({
"aaData": test,
"aoColumns": [
{ "sTitle": "ID" },
{ "sTitle": "parentColumnSortID" },
{ "sTitle": "description" },
{ "sTitle": "columnSortID", "sClass": "center" }
]
});
});

[/code]

Replies

  • aaronwaaronw Posts: 89Questions: 3Answers: 4
    edited August 2011
    Your JSON has column names in it - ID, parentColumnSortID, columnSortID, itemtype, etc. You just want the raw data in each array, per column. It should just be an array of non-associative arrays
    [["unit2","3"," ","3.2"],["unit3","4"," ",3.3"]]

    (or mDataProp, as Greg points out... depends which way is faster)
  • jagdipajagdipa Posts: 14Questions: 0Answers: 0
    I need the column names in the JSON - I am manipulating the JSON according to user actions.
  • GregPGregP Posts: 500Questions: 10Answers: 0
    edited August 2011
    [update: ignore this whole reply and skip to the one below. Keeping it intact though because I'm not ashamed of dumb ideas. ;-) ]

    It's always a pain in the butt trying to figure out how to both store and read associative objects and arrays in JavaScript.

    Judging by the error, you are asking for an element at an index (hence the '0') instead of at its name value. But without seeing the code that's actually processing the request, it's hard to say. Note that the error isn't saying it's invalid JSON, which it will normally do when not parsed correctly. So it's either the pattern of storing or the syntax of retrieving that's breaking.

    [update: had another look at the JavaScript example; I would do it the way you're doing it, too. I'm not too helpful here. For what it's worth, I use named columns, too.]

    Shot in the dark here, but I have a separate Ajax call on my page (not using DataTables) that uses an associative object, and it did not work unless I declared its type first:

    [code]
    var myObj = {};
    myObj = {{"stuff":"things"},{"stuff":"otherthings"}};
    [/code]

    Might seem silly, but have you tried var test=[]; test=[stuff]?
  • GregPGregP Posts: 500Questions: 10Answers: 0
    Got it! Without mDataProp defined, it is using numerical indexing. So, for each of your columns you just need to declare an mDataProp.

    [code]
    "aoColumns": [
    { "mDataProp": "ID",
    "sTitle": "ID" },
    {
    "mDataProp": "parentColumnSortID",
    "sTitle": "parentColumnSortID" },
    {
    "mDataProp": "description",
    "sTitle": "description" },
    {
    "mDataProp": "columnSortID",
    "sTitle": "columnSortID", "sClass": "center" }
    ]
    [/code]
  • jagdipajagdipa Posts: 14Questions: 0Answers: 0
    GregP - That is excellent. Thank you.
This discussion has been closed.