Skip to content
Browse files

完成json的解析

  • Loading branch information...
1 parent 31bc747 commit 978672c5b306b59bd7f1f03e84d82d34e070c7fa azrael committed Mar 31, 2013
Showing with 104 additions and 1 deletion.
  1. +73 −1 jsonparser.js
  2. +31 −0 test-json.html
View
74 jsonparser.js
@@ -9,7 +9,79 @@
}
JSONParser.prototype.parse = function(jsonText) {
- // body...
+ // throw new Error('not complete');
+ var preter = this._interpreter;
+ preter.prepare(jsonText);
+ var result = {};
+ var token = preter.eatUntil('{');//找到第一个 { ,作为json的开始
+ if(token === null){
+ throw new Error('not json');
+ }
+ preter.eat();
+ handleProperties(result, preter);
+ return result;
};
+ function handleProperties(obj, preter){
+ var token, str, prop, value, tmpObj;
+ while((token = preter.eatUntil(['}', '"'])) !== null){
+ str = preter.eat();
+ if(str === '"'){//property start
+ prop = preter.eatUntil(str);
+ preter.eatUntil(':');
+ preter.eat();//吞掉:
+ token = preter.eatUntil([',', '{', '}', '[']);
+ str = preter.eat();//吞掉,或}{
+ if(str === ','){
+ value = getValue(token);
+ obj[prop] = value;
+ }else if(str === '}'){
+ value = getValue(token);
+ obj[prop] = value;
+ return;//这个obj已经结束了
+ }else if(str === '{'){//嵌套的obj
+ value = {};
+ obj[prop] = value;
+ handleProperties(value, preter);
+ }else if(str === '['){
+ value = [];
+ obj[prop] = value;
+ while((token = preter.eatUntil(['{', ']'])) !== null){
+ str = preter.eat();
+ if(str === ']'){
+ //数组结束了
+ break;
+ }else if(str === '{'){
+ tmpObj = {};
+ value.push(tmpObj);
+ handleProperties(tmpObj, preter);
+ }
+ }
+ }
+ }else if(str === '}'){
+ //obj结束了
+ return;
+ }
+ }
+ }
+
+ function getValue(str){
+ //目前不支持Date类型
+ str = str.trim();
+ if(str === 'null'){
+ return null;
+ }else if(str.indexOf('"') === 0){
+ //string
+ return str.substring(1, str.length - 1);
+ }else if(str === 'true'){
+ return true;
+ }else if(str === 'false'){
+ return false;
+ }else {
+ return parseFloat(str);
+ }
+ }
+
+ this.JSONParser = JSONParser;
+
});
View
31 test-json.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+ <title>Test</title>
+</head>
+<body>
+<textarea id="xmlText" style="width: 520px; height: 60px;">
+{"nodeType":9,"nodeName":"document","attributes":{},"childNodes":[{"nodeName":"xml","nodeType":10,"nodeValue":"version=\"1.0\""},{"nodeName":"DOCTYPE","nodeType":10,"nodeValue":"html"},{"nodeType":1,"nodeName":"head","attributes":{},"childNodes":[{"nodeType":1,"nodeName":"meta","attributes":{"http-equiv":"content-type","content":"text/html; charset=utf-8"},"childNodes":[],"tagName":"meta"},{"nodeType":1,"nodeName":"title","attributes":{},"childNodes":[{"nodeType":3,"nodeValue":"Test"}],"tagName":"title"}],"tagName":"head"},{"nodeType":8,"nodeValue":" <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n <title>Test</title>"}]}
+</textarea>
+
+<button id="action">do parse</button>
+<div id="result"></div>
+<script src="ztool.mini.js"></script>
+<script src="interpreter.js"></script>
+<script src="jsonparser.js"></script>
+<script>
+Z.$package(function(z){
+ var txt = document.getElementById('xmlText');
+ var btn = document.getElementById('action');
+ var parser = new z.parser.JSONParser();
+
+ btn.onclick = function(){
+ ret = parser.parse(txt.value);
+ console.log(ret);
+ }
+
+});
+</script>
+</body>
+</html>

0 comments on commit 978672c

Please sign in to comment.
Something went wrong with that request. Please try again.