package com.hy;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

class Token{
    static final int TYPE_LBRACE=0;// 左大括号
    static final int TYPE_RBRACE=1;// 右大括号
    static final int TYPE_TEXT=2;// 文本
    static final int TYPE_COMMA=3;// 逗号
    static final int TYPE_COLON=4;// 冒号
    static final int TYPE_LBRACKET=5;// 左中括号
    static final int TYPE_RBRACKET=6;// 右中括号

    int type;
    String text;

    public Token(char c,int type) {
        this.text=String.valueOf(c);
        this.type=type;
    }

    public Token(String word,int type) {
        this.text=word;
        this.type=type;
    }
}
/**
 * Json文本分词器
 * @author 逆火
 *
 * 2019年12月1日 上午11:35:43
 */
public class Lexer {
    private List<Token> tokenList;

    /**
     * Contructor
     * @param jsonStr
     */
    public Lexer(String jsonStr) {
        tokenList=new ArrayList<Token>();

        String line="";
        for(int i=0;i<jsonStr.length();i++){
            char c=jsonStr.charAt(i);

            if(Character.isWhitespace(c)){
                continue;
            }else if(c=='{'){
                Token t=new Token(c,Token.TYPE_LBRACE);
                tokenList.add(t);
            }else if(c=='}'){
                if(StringUtils.isNotEmpty(line)) {
                    Token w=new Token(line,Token.TYPE_TEXT);
                    tokenList.add(w);
                    line="";
                }

                Token t=new Token(c,Token.TYPE_RBRACE);
                tokenList.add(t);
            }else if(c=='['){
                Token t=new Token(c,Token.TYPE_LBRACKET);
                tokenList.add(t);
            }else if(c==']'){
                Token t=new Token(c,Token.TYPE_RBRACKET);
                tokenList.add(t);
            }else if(c==',') {
                if(StringUtils.isNotEmpty(line)) {
                    Token w=new Token(line,Token.TYPE_TEXT);
                    tokenList.add(w);
                    line="";
                }

                Token t=new Token(c,Token.TYPE_COMMA);
                tokenList.add(t);
            }else if(c==':') {
                if(StringUtils.isNotEmpty(line)) {
                    Token w=new Token(line,Token.TYPE_TEXT);
                    tokenList.add(w);
                    line="";
                }

                Token t=new Token(c,Token.TYPE_COLON);
                tokenList.add(t);
            }else {
                line+=c;
            }
        }
    }

    public List<Token> getTokenList() {
        return tokenList;
    }

    public void printTokens() {
        int idx=0;
        for(Token t:tokenList) {
            idx++;
            System.out.println("#"+idx+" "+t.text);
        }
    }

    /**
     * Entry point
     */
    public static void main(String[] args) {
        String filePathname="D:\\logs\\1.json";
        try {
            StringBuilder sb=new StringBuilder();
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePathname), "UTF-8"));
            String line = null;
            while( ( line = br.readLine() ) != null ) {
                sb.append(line);
            }
            br.close();  

            String jsonStr=sb.toString();
            System.out.println("Raw json="+jsonStr);

            Lexer l=new Lexer(jsonStr);
            l.printTokens();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

输出:

Raw json={    "status": "0000",    "message": "success",    "data": {        "title": {            "id": "001",            "name" : "白菜"        },        "content": [            {                "id": "001",                "value":"你好 白菜"            },            {                "id": "002",                 "value":"你好 萝卜"             }        ]    }}
#1 {
#2 "status"
#3 :
#4 "0000"
#5 ,
#6 "message"
#7 :
#8 "success"
#9 ,
#10 "data"
#11 :
#12 {
#13 "title"
#14 :
#15 {
#16 "id"
#17 :
#18 "001"
#19 ,
#20 "name"
#21 :
#22 "白菜"
#23 }
#24 ,
#25 "content"
#26 :
#27 [
#28 {
#29 "id"
#30 :
#31 "001"
#32 ,
#33 "value"
#34 :
#35 "你好白菜"
#36 }
#37 ,
#38 {
#39 "id"
#40 :
#41 "002"
#42 ,
#43 "value"
#44 :
#45 "你好萝卜"
#46 }
#47 ]
#48 }
#49 }

--END-- 2019年12月1日12:29:00

最新文章

  1. Boolean()值为false的五个特殊值
  2. C++ Primer Plus 第六版笔记
  3. [LeetCode] All solution
  4. 关于《selenium2自动测试实战--基于Python语言》
  5. UE3植被工具-支持刷Actor)
  6. MPlayer-ww 增加边看边剪切功能
  7. ActionScript ArrayCollection sort
  8. c++,内联成员函数
  9. js面向对象学习笔记(四):对象的混合写法
  10. selemiun 自动化测试登录验证码处理
  11. Android为TV端助力 最详细的动画大全,包括如何在代码和在XML中使用
  12. windows 10 &amp; task view &amp; shortcut
  13. BZOJ4695 最假女选手(势能线段树)
  14. [about remote controller]--mstsc-teamviewer-vnc,nomachine
  15. 2017-6-6&amp;6-8/大型网站架构总结
  16. (原创)拨开迷雾见月明-剖析asio中的proactor模式(一)
  17. Spark SQL编程指南(Python)【转】
  18. TIP协议
  19. weblogic连接池问题总结(转载)
  20. JavaScript(二)-精简

热门文章

  1. python之编码与解码、is 与==的区别
  2. python获取第前多少天的日期
  3. Linux 永久改变系统时间
  4. python算法与数据结构-选择排序算法(33)
  5. ASP.NET MVC 入门11、使用AJAX
  6. springboot框架笔记
  7. IO多路复用的作用?
  8. 2019/10/22 test T1 题解
  9. S1_搭建分布式OpenStack集群_02 虚拟机环境配置
  10. asp.net大文件分块上传断点续传demo