[英]xml to json to javascript
my xml : 我的xml:
...
<phone type="cell" ext=""></phone>
<phone type="work" ext="">(123) 456 7890</phone>
...
in php; 在PHP中; I json_encode($xml) and echo to browser.
我json_encode($ xml)并回显到浏览器。 the browser recieves:
浏览器收到:
...
"phone": [
{
"@attributes": {
"type": "cell",
"ext": ""
}
}, "(123) 456 7890", {
"@attributes": {
"type": "work",
"ext": ""
}
}
]
in javascript 在JavaScript中
...
var phone_rec = {};
phone_rec.addPhone = function(argument, sender) {
function newPhone() {
this.phone = {};
this.phone.@attributes = {};
this.phone.@attributes.type = null;
this.phone.@attributes.ext = null;
};
return newPhone();
}...
this falls apart and I am unable to reference this.phone for the number . 这分崩离析,我无法参考这个电话。
That's because you don't call newPhone
on your object, by default this
will equal to window
however. 那是因为您没有在对象上调用
newPhone
,但是默认情况下, this
等于window
。 Call it like this instead: 像这样调用它:
return newPhone.call(this);
See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call for documentation. 有关文档,请参见https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call 。
In addition, phone.@attributes
will produce a syntax error as noted correctly by Felix Kling (identifiers cannot contain @
). 此外,
phone.@attributes
会产生Felix Kling正确指出的语法错误(标识符不能包含@
)。 You should access this property like this: 您应该像这样访问此属性:
this.phone["@attributes"].type = null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.