JSON解析 - java库org.json使用

先把项目文件放上来(gif文件另存为zip即可)

http://hi.csdn.net/attachment/201107/25/0_1311594988iFs9.gif


json表达式定义


Json 的基本文法是键值对,Key:Value 通过冒号分隔


key类型就是字符串 “key1“, ”key2“。。。


关键是值类型,json定义了7种值类型:


1) 字符串,2) 数值,3) true, 4) false, 5) null, 甚至 6) 数组 7) 对象 都可以是值类型。



数组也是一种值,数组同时可以用来包含一系列的值


对象类型也是一种值,对象可以用来包含一系列的键值对


JSON 文件例子, sample.json

{
    "key_a":"val_string",     
    "key_b":100.0,    
    "key_c":20,    
    "key_d":true,    
    "key_f":false,    
    "key_g":null,    
    "key_h":
    {
        "key_h1":"h1", 
        "key_h2":100.0,
        "key_h3":20,
        "key_h4":true,
        "key_h5":false,
        "key_h6":null, 
        "key_arrary":[1, 2, 3, 4, 5]
    },
    "key_i":
    [
        "string", 
        100.0,
        20,
        true, 
        false, 
        null, 
        {"key_1":1, "key_2":2, "key_3":3}
    ]
}


解析JSONObject, depth参数用于打印缩进格式用。

    private void parseJsonObject(JSONObject jo, int depth)
    {
        JSONArray ja = jo.names();
        for (int i = 0; i < ja.length(); i ++)
        {
            String strKey = ja.optString(i);
            String strRawJson = jo.opt(strKey).toString();
            char cTestChar = (strRawJson.charAt(0));
            switch (cTestChar)
            {
            case '{':
                for (int xxx = 0; xxx < depth; xxx++) System.out.print("\t");
                System.out.println(strKey + " value is " + "Object");
                parseJsonObject(jo.optJSONObject(strKey), depth+1);
                break;
            case '[':
                for (int xxx = 0; xxx < depth; xxx++) System.out.print("\t"); 
                System.out.println(strKey + " value is " + "Array");
                parseJsonArray(jo.optJSONArray(strKey), depth+1);
                break;
            default:
                for (int xxx = 0; xxx < depth; xxx++) System.out.print("\t"); 
                System.out.println(strKey + " value is " + strRawJson);
                break;
            }
        }
    }
  


解析JSONArray, depth参数用于打印缩进格式用。

    private void parseJsonArray(JSONArray ja, int depth)
    {
        for (int i = 0; i < ja.length(); i ++)
        {
            String strRawJson = ja.opt(i).toString();
            char cTestChar = (strRawJson.charAt(0));
            switch (cTestChar)
            {
            case '{':
                for (int xxx = 0; xxx < depth; xxx++) System.out.print("\t"); 
                System.out.println("[" + i + "]" + " value is " + "Object");
                parseJsonObject(ja.optJSONObject(i), depth+1);
                break;
            case '[':
                for (int xxx = 0; xxx < depth; xxx++) System.out.print("\t"); 
                System.out.println("[" + i + "]" + " value is " + "Array");                
                parseJsonArray(ja.optJSONArray(i), depth+1);
                break;
            default:
                for (int xxx = 0; xxx < depth; xxx++) System.out.print("\t"); 
                System.out.println("[" + i + "]" + " value is " + strRawJson);
                break;
            }
        }
    }   

测试函数:

    private void parseJsonFile()
    {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(".\\raw\\sample.json"));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (null == br)
            return ;
        
        String line = null;
        String strJson = new String();
        try {
            while (null != (line = br.readLine()))
                strJson += line;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        try {
            br.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        JSONObject root = null;        
        try {
            root = new JSONObject(strJson);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (root == null)
            return ;
        
        parseJsonObject(root, 0);
    }

执行结果:

key_i value is Array
    [0] value is string
    [1] value is 100.0
    [2] value is 20
    [3] value is true
    [4] value is false
    [5] value is null
    [6] value is Object
        key_3 value is 3
        key_2 value is 2
        key_1 value is 1
key_h value is Object
    key_arrary value is Array
        [0] value is 1
        [1] value is 2
        [2] value is 3
        [3] value is 4
        [4] value is 5
    key_h1 value is h1
    key_h2 value is 100.0
    key_h3 value is 20
    key_h4 value is true
    key_h5 value is false
    key_h6 value is null
key_c value is 20
key_b value is 100.0
key_a value is val_string
key_g value is null
key_f value is false
key_d value is true



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值