The project I am working on requires alot of work with somewhat complex XML files, so I convert them into SimpleXML array, JSON Encode it and pass it to Knockout via Ajax.
The problem is that pretty much each property has attributes, which SimpleXML shoves into an object called @attributes. When I try to bind to it like myObject().@attributes.Name, I get an error
Message: SyntaxError: Unexpected token ILLEGAL;
Bindings value: text: alertObj().@attributes.Name
I have tried '@attributes', ['@attributes'], [@attributes] but nothing is working. I have also tried various escape characters and unicode escapes.
This is what json looks like:
var alertObject =
{
"@attributes":
{
"DescriptionContentType":"text\/plain",
"Description":"",
"IsActive":"true",
"Name":"Apache Requests Per Interval"
},
"Frequency":
{
"PeriodInSeconds":"60"
},
"MetricLevelNotification":"false",
"AlertTriggerMode":"2",
"CautionActionDelay":"0",
"DangerActionDelay":"0",
"CautionActionList":
{
"ActionID":
{
"ManagementModuleName":"Base Module",
"ConstructName":"Base SMTP Mail Action"
}
},
"DangerActionList":
{
"ActionID":
{
"ManagementModuleName":"Base Module",
"ConstructName":"Base SMTP Mail Action"
}
},
"MetricGroupingID":
{
"ManagementModuleName":"Base Module",
"ConstructName":"Apache Requests Per Interval"
},
"AlertCombineOperator":"1",
"AlertCompareOperator":"2",
"CautionTargetValue":"2000",
"DangerTargetValue":"15000",
"CautionMinNumPerPeriod":"3",
"CautionAlertPeriod":"6",
"DangerMinNumPerPeriod":"1",
"DangerAlertPeriod":"1"
};
I would like to avoid altering structure and names in any way.
How can I get this to work??
That is because it is not a valid javascript variable name:
from http://mathiasbynens.be/notes/javascript-identifiers
A variable name cannot begin with a @ and hence you see the error. There is no point in appending single quotes, brackets or anything else. The variable name simply isn’t valid and you need to change the way the data is returned from server.