开发者

xml to json to javascript

开发者 https://www.devze.com 2023-03-31 02:45 出处:网络
my xml : ... <phone type=\"cell\" ext=\"\"></phone> <phone type=\"work\" ext=\"\">(123) 456 7890</phone>

my xml :

...
<phone type="cell" ext=""></phone>
<phone type="work" ext="">(123) 456 7890</phone>
...

in php; I json_encode($xml) and echo to browser. the browser recieves:

...
"phone": [
    {
        "@attributes": {
            "type": "cell",
            "ext": ""
        }
    }, "(123) 456 7890", {
        "@a开发者_开发百科ttributes": {
            "type": "work",
            "ext": ""
        }
    }
]

in 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. Call it like this instead:

return newPhone.call(this);

See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call for documentation.

In addition, phone.@attributes will produce a syntax error as noted correctly by Felix Kling (identifiers cannot contain @). You should access this property like this:

 this.phone["@attributes"].type = null;
0

精彩评论

暂无评论...
验证码 换一张
取 消