1. 程式人生 > >用c#實現json解析與序列化及格式化輸出

用c#實現json解析與序列化及格式化輸出

1. 簡介     

  json(javascript object notation)是一種使用可讀文字形式的檔案格式,用於傳輸由key-value對和array陣列形式的資料物件。這種資料格式在非同步的瀏覽器-服務端通訊模式中經常使用,作為替換xml在ajax中使用。         (摘自wiki,實際上現在的web開發中ajax傳輸大部分都是使用了json格式文字)

      json物件可以看做是一個巢狀存在的object-array-element物件結構。各元素組成格式略為下方列表

     array: [element, element, ......]                    //陣列array是element的一個序列集合,可為空[]

     object : {key-value, key-value, ....}                //物件object是key-value對的一個序列集合,可為空{}

     element: null | bool | int | double | string | object | array 

     key:string                                                   //key只能是字串形式,本文限定為只包含[0-9],[a-z],[A-Z],-,_字元序列

     value : element                                             //value可以為一切基本元素

2. json文字解析

   json文字解析可以按照上述,根據格式分別解析element,object,array

  使用通用object節點儲存每一個element元素,型別使用一個列舉類定義

public enum JsonType
{
    TYPE_NULL = 0,
    TYPE_BOOL = 1,
    TYPE_INT = 2,
    TYPE_DOUBLE = 3,
    TYPE_STRING = 4,
    TYPE_OBJECT_NULL = 10,  //{}
    TYPE_OBJECT = 11,
    TYPE_ARRAY = 12,
}

   節點屬性結構初始化如下

    //包含的資料,包括型別,數值,字串
    public JsonType _type = JsonType.TYPE_NULL;             //預設節點型別為null
    public int _num = 0;                                    //用於儲存bool型和int型資料
    public double _double = 0.0f;                           //用於儲存double型別資料
    public string _string = "";                             //用於儲存string型別資料
    public List<MyJson> list = new List<MyJson>(0);         //用於型別為array的節點儲存子節點元素
    public Dictionary<string, MyJson> child = new Dictionary<string, MyJson>();     //用於object儲存key-value型別

  1. 解析element

  a. null格式

    public static MyJson ParseNull(string s)
    {
        if (s == "null")
            return new MyJson();
        return null;
    }

 b. bool格式

    public static MyJson ParseBool(string s)
    {
        if (s == "true" || s == "false")
        {
            MyJson tmp = new MyJson();
            tmp._type = JsonType.TYPE_BOOL;
            if (s == "true")
            {
                tmp._num = 1;
            }
            else
            {
                tmp._num = 0;
            }
            return tmp;
        }
        return null;
    }
 c. 解析int型別
    public static MyJson ParseInt(string s)
    {
        int index = 0;
        int num = 0;
        bool flag = false;  //是否有負號
        if (s[0] == '+' || s[0] == '-')
        {
            flag = s[0] == '-';
            index++;
        }
        for (int i = index; i < s.Length; ++i)
        {
            if (s[i] > '9' || s[i] < '0')
                return null;
            num = num * 10 + s[i] - '0';
        }
        MyJson tmp = new MyJson();
        tmp._type = JsonType.TYPE_INT;
        tmp._num = flag ? -1 * num : num;
        return tmp;
    }
 d. 解析double型別
    public static MyJson ParseDouble(string s)
    {
        //校驗下是否是double
        int index = 0, cnt = 0;
        if (s[0] == '+' || s[0] == '-') index++;
        for (int i = index; i < s.Length; ++i)
        {
            if (s[i] == '.')
                cnt++;
            else if (s[i] > '9' || s[i] < '0')
                return null;
        }
        if (cnt > 1)
            return null;

        double num = System.Convert.ToDouble(s);
        MyJson tmp = new MyJson();
        tmp._type = JsonType.TYPE_DOUBLE;
        tmp._double = num;
        return tmp;
    }
 e. 解析string型別
    public static MyJson ParseString(string s)
    {
        //判定是否有效字串
        if (s.Length < 2 || s[0] != '\"' || s[s.Length - 1] != '\"')
            return null;
        MyJson tmp = new MyJson();
        tmp._type = JsonType.TYPE_STRING;
        tmp._string = s.Substring(1, s.Length - 2);
        return tmp;
    }

 f. 解析空物件型別

    public static MyJson ParseNullObject(string s)
    {
        if (s != "{}")
            return null;
        MyJson tmp = new MyJson();
        tmp._type = JsonType.TYPE_OBJECT_NULL;
        return tmp;
    }

 對去格式化文字整體解析程式碼如下(即json文字已處理過,不包含多餘的空格,tab,換行以及其他空白可列印字元等)

    //物件,格式必須為{}開頭結尾,中間用,隔開的key:value對
    public static MyJson ParseObject(string s)
    {
        if (s.Length == 0)
            return null;
        if (s == "null")
        {
            return ParseNull(s);
        }
        if (s == "true" || s == "false")
        {
            return ParseBool(s);
        }
        if (s == "{}")
        {
            return ParseNullObject(s);
        }
        if (s[0] == '+' || s[0] == '-' || s[0] >= '0' && s[0] <= '9')
        {
            if (s.IndexOf('.') >= 0)
                return ParseDouble(s);
            return ParseInt(s);
        }
        if (s[0] == '"')
            return ParseString(s);
        if (s.Length < 2)
            return null;
        if (s[0] == '[' && s[s.Length - 1] == ']')
        {
            MyJson tmp = new MyJson();
            tmp._type = JsonType.TYPE_ARRAY;
            int start = 1, end = 1;
            while (end < s.Length - 1)
            {
                while (end < s.Length - 1)
                {
                    if (s[end] == '"')
                    {
                        ++end;
                        while (end < s.Length - 1 && s[end] != '"') ++end;
                    }
                    if (s[end] == '[')
                    {
                        ++end;
                        while (end < s.Length - 1 && s[end] != ']') ++end;
                    }
                    if (s[end] == '{')
                    {
                        ++end;
                        while (end < s.Length - 1 && s[end] != '}') ++end;
                    }
                    if (end == s.Length - 1 || s[end] == ',')
                        break;
                    ++end;
                }
                MyJson subtmp = ParseObject(s.Substring(start, end - start));
                tmp.list.Add(subtmp);
                end++;
                start = end;
            }
            return tmp;
        }
        if (s[0] == '{' && s[s.Length - 1] == '}')
        {

            MyJson tmp = new MyJson();
            tmp._type = JsonType.TYPE_OBJECT;
            int start = 1, end = 1;
            while (end < s.Length - 1)   //到末尾
            {
                while (end < s.Length - 1)   //到逗號或者末尾,一對key:value
                {
                    if (s[end] == '"')
                    {
                        ++end;
                        while (end < s.Length - 1 && s[end] != '"') ++end;
                    }
                    if (s[end] == '[')
                    {
                        ++end;
                        while (end < s.Length - 1 && s[end] != ']') ++end;
                    }
                    if (s[end] == '{')
                    {
                        ++end;
                        while (end < s.Length - 1 && s[end] != '}') ++end;
                    }
                    if (end == s.Length - 1 || s[end] == ',')  //真實逗號,不是在引號裡面的
                        break;
                    ++end;
                }
                string sub = s.Substring(start, end - start);
                string key;
                MyJson subtmp = ParseKeyValue(sub, out key);
                if (subtmp == null)
                    return null;
                if (tmp.child.ContainsKey(key))
                    return null;
                tmp.child.Add(key, subtmp);
                end++;
                start = end;
            }
            return tmp;
        }
        return null;
    }

  這裡有兩個引用方法,一個是解析key-value對的ParseKeyValue,一個是校驗json物件的key是否是合法字串CheckKeyValid,程式碼分別如下

    public static MyJson ParseKeyValue(string s, out string key)
    {
        key = "";
        //獲取key, value
        if (s[0] != '"')
            return null;
        int index = 1;
        while (index < s.Length && s[index] != '"') ++index;
        if (index == s.Length)
            return null;
        key = s.Substring(1, index - 1);
        //校驗key是否合法
        if (!CheckKeyValid(key))
        {
            return null;
        }
        //取出後面的value來
        index++;
        if (index == s.Length || s[index] != ':')
            return null;
        index++;

        string sValue = s.Substring(index, s.Length - index);
        MyJson val = ParseObject(sValue);

        return val;
    }
    public static bool CheckKeyValid(string s)    //在key內只允許存在a-z,A-Z,0-9,-,_
    {
        if (s.Length == 0) return false;
        for (int i = 0; i < s.Length; ++i)
        {
            if (s[i] != '-' && s[i] != '_' && !(s[i] >= '0' && s[i] <= '9') && !(s[i] >= 'a' && s[i] <= 'z') && !(s[i] >= 'A' && s[i] <= 'Z'))
                return false;
        }
        return true;
    }
 g. json文字的預處理(去除多餘空格,換行)
    public static string CondenceString(string s)
    {
        //主要是壓縮非\"\"字串之間的其他符號
        System.IO.StringWriter sw = new System.IO.StringWriter();
        int index = 0;
        while (index < s.Length)
        {
            if (s[index] == '\"')
            {
                sw.Write(s[index]);
                ++index;
                while (index < s.Length && s[index] != '\"')
                {
                    sw.Write(s[index]);
                    ++index;
                }
            }
            //在tab內
            while (index < s.Length && tab.IndexOf(s[index]) >= 0)
                ++index;
            if (index == s.Length) break;
            sw.Write(s[index]);
            ++index;
        }
        return sw.ToString();
    }

  這裡可以預宣告哪些符號是可以省略的,如本文定義為    const string tab = " \t\n\b";(空格,tab,換行,\b可以忽略),處理的主要是在""包含字串外的這些字元.

    至此,文字轉化為json物件的方法已經處理完。

3. json序列化及格式化

json的序列化可以看做是解析的反過程,使用遞迴方式對object做遞迴序列化為字串即可,程式碼如下:

    public string ToStr()
    {
        if (_type == JsonType.TYPE_NULL)
        {
            return "null";
        }
        if (_type == JsonType.TYPE_BOOL)
        {
            return _num == 1 ? "true" : "false";
        }
        if (_type == JsonType.TYPE_INT)
        {
            return _num.ToString();
        }
        if (_type == JsonType.TYPE_DOUBLE)
        {
            return _double.ToString();
        }
        if (_type == JsonType.TYPE_STRING)
        {
            return "\"" + _string + "\"";
        }
        if (_type == JsonType.TYPE_OBJECT_NULL)
        {
            return "{}";
        }
        if (_type == JsonType.TYPE_OBJECT)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            sw.Write("{");
            int cnt = 0;
            foreach (var tmp in child)
            {
                sw.Write("\"");
                sw.Write(tmp.Key);
                sw.Write("\":");
                sw.Write(tmp.Value.ToStr());
                if (++cnt < child.Count)
                    sw.Write(",");
            }
            sw.Write("}");
            return sw.ToString();
        }
        if (_type == JsonType.TYPE_ARRAY)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            sw.Write("[");
            int cnt = 0;
            foreach (var tmp in list)
            {
                sw.Write(tmp.ToStr());
                if (++cnt < list.Count)
                    sw.Write(",");
            }
            sw.Write("]");
            return sw.ToString();
        }
        return "";
    }
帶有格式化的輸出,本文主要是處理換行和縮排關係,預宣告對array的格式化輸出形式和object的格式化輸出形式的空格縮排
    const string objtab = "  ";
    const string arraytab = "  ";
格式化輸出的程式碼如下
   public string ToStrWithFormat()
    {
        if (_type == JsonType.TYPE_NULL)
        {
            return "null";
        }
        if (_type == JsonType.TYPE_BOOL)
        {
            return _num == 1 ? "true" : "false";
        }
        if (_type == JsonType.TYPE_INT)
        {
            return _num.ToString();
        }
        if (_type == JsonType.TYPE_DOUBLE)
        {
            return _double.ToString();
        }
        if (_type == JsonType.TYPE_STRING)
        {
            return "\"" + _string + "\"";
        }
        if (_type == JsonType.TYPE_OBJECT_NULL)
        {
            return "{}";
        }
        if (_type == JsonType.TYPE_OBJECT)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            sw.Write("{\n");
            int cnt = 0;
            foreach (var tmp in child)
            {
                sw.Write(objtab + "\"");
                sw.Write(tmp.Key);
                sw.Write("\":");
                string sub = tmp.Value.ToStrWithFormat();
                Debug.Log("sub" + sub);
                string[] slist = tmp.Value.ToStrWithFormat().Split('\n');
                Debug.Log("slist:" + slist.Length);
                sw.Write(slist[0]);
                if (slist.Length > 2)
                {
                    for (int i = 1; i < slist.Length - 1; ++i)
                        sw.Write("\n" + objtab + slist[i]);
                    sw.Write("\n" + objtab + slist[slist.Length - 1]);
                }
                if (++cnt < child.Count)
                    sw.Write(",");
                sw.Write("\n");
            }
            sw.Write("}");
            return sw.ToString();
        }
        if (_type == JsonType.TYPE_ARRAY)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            sw.Write("[\n");
            int cnt = 0;
            foreach (var tmp in list)
            {
                sw.Write(arraytab);
                //sw.Write(tmp.ToStr());
                string[] slist = tmp.ToStrWithFormat().Split('\n');
                sw.Write(slist[0]);
                if (slist.Length > 2)
                {
                    for (int i = 1; i < slist.Length - 1; ++i)
                        sw.Write("\n" + arraytab + slist[i]);
                    sw.Write("\n" + arraytab + slist[slist.Length - 1]);
                }
                if (++cnt < list.Count)
                    sw.Write(",");
                sw.Write("\n");
            }
            sw.Write("]");
            return sw.ToString();
        }
        return "";
    }
4. json物件的型別判定、基本元素值獲取,根據key下標取值等方法

這些都是基本的處理方式,本文不予詳解了,感興趣的可以點選連結我的碼雲檢視



本想使用c++實現這一功能,發現自己最近工作c#指令碼寫多了,寫c++總有些卡殼,不如這邊方便,於是便改用c#在unity下實現了這一功能,但是目前只有一些基本的功能,可能還有不少bug特殊情況未考慮到,比如說對json文字內註釋的處理,對字串中特殊字元、轉義字元的處理等。這些之後會不斷修改增加。

這份程式碼編寫原因主要是閱讀了大牛的一個json開原始碼cjson,使用c實現,通過理解其對json物件的處理基本方式,自己也寫了個,不得不說,使用高階語言編寫,不需要操作記憶體的獲取釋放,真的是編寫難度要小很多,但是同時執行效率上應該也會差不少。



以後也要經常寫寫自己遊戲開發的一些學習內容、體驗,不斷學習,不斷進步吧。