function
typeOf(value) {
var
s =
typeof
value;
if
(s ===
'object'
) {
if
(value) {
if
( value
instanceof
Array) {
s =
'array'
;
}
}
else
{
s =
'null'
;
}
}
return
s;
}
function
json2xml(obj) {
var
out =
''
;
switch
(typeOf(obj)) {
case
'array'
:
out +=
''
;
for
(
var
i=0; i
out=out +
"- "
+ json2xml(obj[i]) +
"
;
}
out +=
"
;
break
;
case
'object'
:
out +=
''
;
for
(
var
i
in
obj) {
out=out +
"<"
+ i +
">"
+ json2xml(obj[i]) +
"</"
+ i +
">"
;
}
out+=
"
;
break
;
case
'string'
:
out+=
"<![CDATA["
+ obj +
"]]>"
;
break
;
default
:
out += obj;
break
;
}
return
out;
}
var
myJson =
"[{a:1},{a:2},{b:'Rock the fig'}]"
;
var
obj = eval(myJson);
alert(json2xml(obj));
Comments
Post a Comment