{}
{"Key":[值]}的格式,所以先解析到Key字符串,将Key解析出来,然后在解析到值,因为值有可能是【字符串、值类型、布尔类型、对象、数组、null】所以需要根据前缀得到类型,并调用相应的解析方法,循环解析到“}”对象结尾[]
[[值],[值]],因为值有可能是【字符串、值类型、布尔类型、对象、数组、null】所以需要根据前缀得到类型,并调用相应的解析方法,循环解析到]数组结尾\如果遇到,当前字符的下一个字符将是作为普通字符存入结果,如果遇到非转义的 " 字符则退出字符串读取方法,并返回结果[0-9]包括.符号,然后调用转换成double类型方法true 还是 falsenull
null
| 方法名 | 方法作用 | 
|---|---|
| AnalysisJson | 解析JSON字符串为C#数据结构 | 
| AnalysisJsonObject | 解析JSON字符串为对象结构 | 
| AnalysisJsonArray | 解析JSON字符串为数组结构 | 
| ReadElement | 读取出一个JSON结构 | 
| ReadJsonNumber | 读取出一个值类型结构 | 
| ReadJsonNull | 读取出一个 null结构 | 
| ReadJsonFalse | 读取出一个 false结构 | 
| ReadJsonTrue | 读取出一个 true结构 | 
| ReadString | 读取出一个字符串结构 | 
| ReadToNonBlankIndex | 读取到非空白字符下标位置 | 
{"Name":"张三","Age":18}
{发现是JSON对象结构,调用AnalysisJsonObject方法来解析JSON对象格式}对象尾部字符
ReadString来进行解析出Key字符串从而得到Name这个值ReadElement来解析出一个JSON结构
"从而知道这个Value是一个字符串,调用方法ReadString来读取到这个Value的值张三}是,字符代表下面还存在一个Key-Value结构,继续读取ReadString来进行解析出Key字符串从而得到Age这个值ReadElement来解析出一个JSON结构
1是数字,代表下面的这个结构是数值类型调用方法ReadJsonNumber来读取数值类型}是JSON对象的结尾字符,退出JSON对象解析,返回解析的JSON对象结构实例[{"科目":"语文","成绩":99}]
[发现是JSON数组结构,调用方法AnalysisJsonArray方法来解析出JSON数组结构
]数组结构结尾字符
ReadElement方法来解析值{发现是JSON对象类型调用AnalysisJsonObject方法解析JSON对象
ReadString来进行解析出Key字符串从而得到科目这个值ReadElement来解析出一个JSON结构
"从而知道这个Value是一个字符串,调用方法ReadString来读取到这个Value的值语文}是,字符代表下面还存在一个Key-Value结构,继续读取ReadString来进行解析出Key字符串从而得到成绩这个值ReadElement来解析出一个JSON结构
9是数字,代表下面的这个结构是数值类型调用方法ReadJsonNumber来读取数值类型}是JSON对象的结尾字符,退出JSON对象解析,返回解析的JSON对象结构实例]JSON数组的结尾,退出解析JSON数组,返回解析的JSON数组结构实例    /// <summary>
    /// JSON解析类型
    /// </summary>
    public static class JsonConvert
    {
        /// <summary>
        /// 解析JSON
        /// </summary>
        /// <param name="text">待解析的JSON字符串</param>
        /// <returns>解析完成的JSON结构对象</returns>
        public static JsonElement AnalysisJson(string text)
        {
            var index = 0;
            //读取到非空白字符
            ReadToNonBlankIndex(text, ref index);
            if (text[index++] == ‘[‘)
                //解析数组
                return AnalysisJsonArray(text, ref index);
            //解析对象
            return AnalysisJsonObject(text, ref index);
        }
        /// <summary>
        /// 解析JSON对象
        /// </summary>
        /// <param name="text">JSON字符串</param>
        /// <param name="index">开始索引位置</param>
        /// <returns>JSON对象</returns>
        private static JsonObject AnalysisJsonObject(string text, ref int index)
        {
            var jsonArray = new JsonObject();
            do
            {
                ReadToNonBlankIndex(text, ref index);
                if (text[index] != ‘"‘) throw new JsonAnalysisException($"不能识别的字符“{text[index]}”!应为“\"”",index);
                index++;
                //读取字符串
                var name = ReadString(text, ref index);
                ReadToNonBlankIndex(text, ref index);
                if (text[index] != ‘:‘) throw new JsonAnalysisException($"不能识别的字符“{text[index]}”!",index);
                index++;
                ReadToNonBlankIndex(text, ref index);
                if (jsonArray.ContainsKey(name)) throw new JsonAnalysisException($"已经添加键值:“{name}”",index);
                //读取下一个Element
                jsonArray.Add(name, ReadElement(text, ref index));
                //读取到非空白字符
                ReadToNonBlankIndex(text, ref index);
            } while (text[index++] != ‘}‘);
            return jsonArray;
        }
        /// <summary>
        /// 解析JSON数组
        /// </summary>
        /// <param name="text">JSON字符串</param>
        /// <param name="index">开始索引位置</param>
        /// <returns>JSON数组</returns>
        private static JsonArray AnalysisJsonArray(string text, ref int index)
        {
            var jsonArray = new JsonArray();
            do
            {
                ReadToNonBlankIndex(text, ref index);
                //读取下一个Element
                jsonArray.Add(ReadElement(text, ref index));
                //读取到非空白字符
                ReadToNonBlankIndex(text, ref index);
            } while (text[index++] != ‘]‘);
            return jsonArray;
        }
        /// <summary>
        /// 读取JSONElement
        /// </summary>
        /// <param name="text">字符串</param>
        /// <param name="index">开始下标</param>
        /// <returns>下一个Element</returns>
        private static JsonElement ReadElement(string text, ref int index)
        {
            switch (text[index++])
            {
                case ‘[‘:
                    return AnalysisJsonArray(text, ref index);
                case ‘{‘:
                    return AnalysisJsonObject(text, ref index);
                case ‘"‘:
                    return new JsonString(ReadString(text, ref index));
                case ‘t‘:
                    return ReadJsonTrue(text, ref index);
                case ‘f‘:
                    return ReadJsonFalse(text, ref index);
                case ‘n‘:
                    return ReadJsonNull(text, ref index);
                case ‘0‘:
                case ‘1‘:
                case ‘2‘:
                case ‘3‘:
                case ‘4‘:
                case ‘5‘:
                case ‘6‘:
                case ‘7‘:
                case ‘8‘:
                case ‘9‘:
                    return ReadJsonNumber(text, ref index);
                default:
                    throw new JsonAnalysisException($"未知Element“{text[index]}”应该为【[、{{、\"、true、false、null】", index);
            }
        }
        /// <summary>
        /// 读取值类型
        /// </summary>
        /// <param name="text">JSON字符串</param>
        /// <param name="index">开始索引</param>
        /// <returns>JSON数值类型</returns>
        private static JsonNumber ReadJsonNumber(string text, ref int index)
        {
            var i = index;
            while (i < text.Length && char.IsNumber(text[i]) || text[i] == ‘.‘) i++;
            if (double.TryParse(text.Substring(index - 1, i - index + 1), out var value))
            {
                index = i;
                return new JsonNumber(value);
            }
            throw new JsonAnalysisException("不能识别的数字类型!", i);
        }
        /// <summary>
        /// 读取NULL
        /// </summary>
        /// <param name="text">JSON字符串</param>
        /// <param name="index">开始索引</param>
        /// <returns>读取NULL</returns>
        private static JsonNull ReadJsonNull(string text, ref int index)
        {
            if (text[index++] == ‘u‘ &&
                text[index++] == ‘l‘ &&
                text[index++] == ‘l‘)
            {
                return new JsonNull();
            }
            throw new JsonAnalysisException("读取null出错!", index);
        }
        /// <summary>
        /// 读取FALSE
        /// </summary>
        /// <param name="text">JSON字符串</param>
        /// <param name="index">开始索引</param>
        /// <returns>布尔值-假</returns>
        private static JsonBoolean ReadJsonFalse(string text, ref int index)
        {
            if (text[index++] == ‘a‘ &&
                text[index++] == ‘l‘ &&
                text[index++] == ‘s‘ &&
                text[index++] == ‘e‘)
            {
                return new JsonBoolean(false);
            }
            throw new JsonAnalysisException("读取布尔值出错!", index);
        }
        /// <summary>
        /// 读取TRUE
        /// </summary>
        /// <param name="text">JSON字符串</param>
        /// <param name="index">开始索引</param>
        /// <returns>布尔值-真</returns>
        private static JsonBoolean ReadJsonTrue(string text, ref int index)
        {
            if (text[index++] == ‘r‘ &&
                text[index++] == ‘u‘ &&
                text[index++] == ‘e‘)
            {
                return new JsonBoolean(true);
            }
            throw new JsonAnalysisException("读取布尔值出错!",index);
        }
        /// <summary>
        /// 读取字符串
        /// </summary>
        /// <param name="text">JSON字符串</param>
        /// <param name="index">开始索引</param>
        /// <returns>字符串值</returns>
        private static string ReadString(string text, ref int index)
        {
            //是否处于转义状态
            var value = new StringBuilder();
            while (index < text.Length)
            {
                var c = text[index++];
                if (c == ‘\\‘)
                {
                    value.Append(‘\\‘);
                    if (index >= text.Length)
                        throw new JsonAnalysisException("未知的结尾!",index);
                    c = text[index++];
                    value.Append(c);
                    if (c == ‘u‘)
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            c = text[index++];
                            if (IsHex(c))
                            {
                                value.Append(c);
                            }
                            else
                            {
                                throw new JsonAnalysisException("不是有效的Unicode字符!",index);
                            }
                        }
                    }
                }
                else if (c == ‘"‘)
                {
                    break;
                }
                else if (c == ‘\r‘ || c == ‘\n‘)
                {
                    throw new JsonAnalysisException("传入的JSON字符串内容中不允许有换行!",index);
                }
                else
                {
                    value.Append(c);
                }
            }
            return value.ToString();
        }
        /// <summary>
        /// 判断是否为16进制字符
        /// </summary>
        private static bool IsHex(char c)
        {
            return c >= ‘0‘ && c <= ‘9‘ || c >= ‘a‘ && c <= ‘z‘ || c >= ‘A‘ && c <= ‘Z‘;
        }
        /// <summary>
        /// 读取到非空白字符
        /// </summary>
        /// <param name="text">字符串</param>
        /// <param name="index">开始下标</param>
        /// <returns>非空白字符下标</returns>
        private static void ReadToNonBlankIndex(string text, ref int index)
        {
            while (index < text.Length && char.IsWhiteSpace(text[index])) index++;
        }
    }
Github项目地址(会持续更新):DEMO代码
原文:https://www.cnblogs.com/liuzhenliang/p/AnalysisJson.html