Symbian WRT中使用JS自动将XML、JSON转换成为对象
2010-08-30 00:03:00 来源:WEB开发网for (var j = 0; j < optionsLength; j++) {
var option = selfProcessData.options[j];
//要从XML读取的节点名称
var nodeName = option['nodeName'];
//取得节点元素
var da = items[i].getElementsByTagName(nodeName)[0];
//通过options取得对应的成员变量名称
var memberName = option['memberName'];
var nodeValueOrAttribute = option['nodeValueOrAttribute'];
var attributeName = option['attributeName'];
if (da != null || da != undefined) {
//判断是取节点的值还是其中的属性值
if (nodeValueOrAttribute == 'attribute' &&
(attributeName != undefined || attributeName != null)) {
selfProcessData.data1.putProperty(memberName, da.getAttribute(attributeName));
}
else {
if (da.firstChild == null || da.firstChild == undefined) {
selfProcessData.data1.putProperty(memberName, null);
}
else {
selfProcessData.data1.putProperty(memberName, da.firstChild.nodeValue);
}
}
}
else {
selfProcessData.data1.putProperty(memberName, null);
}
}
array[i] = selfProcessData.data1;
}
selfProcessData.processDataFunction(array);
}
json2.js - 用于处理JSON数据
Js代码
/**
* 将JSON转换成为string对象
*/
jsonToString = function(obj){
var THIS = this;
switch(typeof(obj)){
case 'string':
return '"' + obj.replace(/(["\])/g, '\$1') + '"';
case 'array':
return '[' + obj.map(THIS.jsonToString).join(',') + ']';
case 'object':
if(obj instanceof Array){
var strArr = [];
var len = obj.length;
for(var i=0; i
更多精彩
赞助商链接