[ActionScript 3.0] AS3 实现XML转换成JSON
1 package com.fylib.util 2 { 3 /** 4 * @author Frost.Yen 5 * @E-mail 871979853@qq.com 6 * @create 2015-6-18 下午2:06:01 7 * 8 */ 9 public class XML2JSON 10 { 11 private static var _arrays:Array; 12 public function XML2JSON() 13 { 14 15 } 16 public static function parse(node:*):Object { 17 var obj:Object = {}; 18 var numOfChilds:int = node.children().length(); 19 for(var i:int = 0; i<numOfChilds; i++) { 20 var childNode:XML = node.children()[i] as XML; 21 var childNodeName:String = childNode.name(); 22 var value:*; 23 if(childNode.children().length() == 1 && childNode.children()[0].name() == null) { 24 if(childNode.attributes().length() > 0) { 25 value = { 26 _content: childNode.children()[0].toString() 27 }; 28 var numOfAttributes:int = childNode.attributes().length(); 29 for(var j:int=0; j<numOfAttributes; j++) { 30 value[childNode.attributes()[j].name().toString()] = childNode.attributes()[j].toString(); 31 } 32 } else { 33 value = childNode.children()[0].toString(); 34 } 35 } else { 36 value = parse(childNode); 37 } 38 if(obj[childNodeName]) { 39 if(getTypeof(obj[childNodeName]) == "array") { 40 obj[childNodeName].push(value); 41 } else { 42 obj[childNodeName] = [obj[childNodeName], value]; 43 } 44 } else if(isArray(childNodeName)) { 45 obj[childNodeName] = [value]; 46 } else { 47 obj[childNodeName] = value; 48 } 49 } 50 numOfAttributes = node.attributes().length(); 51 for(i=0; i<numOfAttributes; i++) { 52 obj[node.attributes()[i].name().toString()] = node.attributes()[i].toString(); 53 } 54 if(numOfChilds == 0) { 55 if(numOfAttributes == 0) { 56 obj = ""; 57 } else { 58 obj._content = ""; 59 } 60 } 61 return obj; 62 } 63 public static function get arrays():Array { 64 if(!_arrays) { 65 _arrays = []; 66 } 67 return _arrays; 68 } 69 public static function set arrays(a:Array):void { 70 _arrays = a; 71 } 72 private static function isArray(nodeName:String):Boolean { 73 var numOfArrays:int = _arrays ? _arrays.length : 0; 74 for(var i:int=0; i<numOfArrays; i++) { 75 if(nodeName == _arrays[i]) { 76 return true; 77 } 78 } 79 return false; 80 } 81 private static function getTypeof(o:*):String { 82 if(typeof(o) == "object") { 83 if(o.length == null) { 84 return "object"; 85 } else if(typeof(o.length) == "number") { 86 return "array"; 87 } else { 88 return "object"; 89 } 90 } else { 91 return typeof(o); 92 } 93 } 94 95 public static function printJSON(o:Object):void { 96 //trace("Debug.printJSON"); 97 //trace(parseJSON(o)); 98 } 99 public static function parseJSON(o:*, spaces:int = 1):String { 100 var str:String = ""; 101 if(getTypeof(o) == "object") { 102 str += "{\n"; 103 for(var i:* in o) { 104 str += getSpaces(spaces) + i + "="; 105 if(getTypeof(o[i]) == "object" || getTypeof(o[i]) == "array") { 106 str += parseJSON(o[i], spaces + 1) + "\n"; 107 } else { 108 var type:String = getTypeof(o[i]); 109 if(type == "string") { 110 str += "\"" + o[i] + "\"\n"; 111 } else if(type == "number") { 112 str += o[i] + "\n"; 113 } 114 } 115 } 116 str += getSpaces(spaces - 1 < 0 ? 0 : spaces - 1) + "}"; 117 } else if(getTypeof(o) == "array") { 118 str += "[\n"; 119 var n:int = o.length; 120 for(i=0; i<n; i++) { 121 str += getSpaces(spaces) + "[" + i + "]="; 122 if(getTypeof(o[i]) == "object" || getTypeof(o[i]) == "array") { 123 str += parseJSON(o[i], spaces + 1) + "\n"; 124 } else { 125 type = getTypeof(o[i]); 126 if(type == "string") { 127 str += "\"" + o[i] + "\""; 128 } else if(type == "number") { 129 str += o[i]; 130 } 131 str += "\n"; 132 } 133 } 134 str += getSpaces(spaces - 1 < 0 ? 0 : spaces - 1) + "]"; 135 } 136 return str; 137 } 138 private static function getSpaces(n:int):String { 139 var str:String = ""; 140 for(var i:int=0; i<n; i++) { 141 str += " "; 142 } 143 return str; 144 } 145 // private static function getTypeof(o:*):String { 146 // return typeof(o) == "object" ? (o.length == null ? "object" : "array") : typeof(o); 147 // } 148 } 149 }
------------------------------------------------------------------
Always put yourself in the other's shoes.If you feel that it hurts you,it probably hurts others,too.------------------------------------------------------------------