自用Json解析

将Json解析成 Key 为字符串,Value 为 object 的 Dictionary


        /// <summary>
        /// 自用型解析Json 3.0
        /// </summary>
        public object GetJsonValue(string json)
        {
            char[] charArr = json.ToCharArray();
            int index = 0;
            if (charArr.Length == 0)
            {
                return null;
            }
            else if (charArr[index] == '{')
            {
                return AnalysisObject(ref charArr, ref index);
            }
            else if (charArr[index] == '[')
            {
                return AnalysisJsonArr(ref charArr, ref index);
            }
            else if (charArr[index] == '\"' || (charArr[index] >= '0' && charArr[index] <= '9'))
            {
                return GetValue(ref charArr, ref index);
            }
            else
            {
                return null;
            }

        }

        /// <summary>
        /// 解析Json对象
        /// </summary>
        /// <param name="charArr"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public Dictionary<string, object> AnalysisObject(ref char[] charArr, ref int index)
        {
            Dictionary<string, object> kv = null;
            if (charArr[index++] == '{')
            {
                kv = new Dictionary<string, object>();
                string key = null;

                while (index < charArr.Length && charArr[index] != '}')
                {
                    if (key == null && charArr[index] == '\"')
                    {
                        key = GetValue(ref charArr, ref index).ToString();
                    }
                    else if (key != null && charArr[index] == ':')
                    {
                        index++;
                        if (charArr[index] == '\"' || (charArr[index] >= '0' && charArr[index] <= '9'))
                        {
                            kv.Add(key, GetValue(ref charArr, ref index));
                        }
                        else if (charArr[index] == '{')
                        {
                            kv.Add(key, AnalysisObject(ref charArr, ref index));
                        }
                        else if (charArr[index] == '[')
                        {
                            kv.Add(key, AnalysisJsonArr(ref charArr, ref index));
                        }
                        key = null;
                    }
                    else if (charArr[index] == ',')
                    {
                        index++;
                    }
                    else //异常
                    {
                        //throw new Exception();
                        break;
                    }
                }
                if (charArr[index] == '}')
                {
                    index++;
                }
            }
            return kv;
        }
        /// <summary>
        /// 获得Json的值
        /// </summary>
        /// <param name="charArr"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public object GetValue(ref char[] charArr, ref int index)
        {
            StringBuilder val = new StringBuilder();
            if (charArr[index] >= '0' && charArr[index] <= '9')
            {
                bool isInt = true;
                while (index < charArr.Length && (charArr[index] >= '0' && charArr[index] <= '9' || charArr[index] == '.'))
                {
                    if (charArr[index] == '.')
                    {
                        isInt = false;
                    }
                    val.Append(charArr[index++]);
                }
                return isInt ? int.Parse(val.ToString()) : double.Parse(val.ToString());
            }
            else if (charArr[index++] == '\"')
            {
                while (index < charArr.Length && charArr[index] != '\"')
                {
                    val.Append(charArr[index++]);
                }
                if (charArr[index] == '\"')
                    index++;
                return val.ToString();
            }
            else//异常
            {
                //throw new Exception();
                return null;
            }
        }
        /// <summary>
        /// 解析Json数组
        /// </summary>
        /// <param name="charArr"></param>
        /// <param name="index"></param>
        private List<object> AnalysisJsonArr(ref char[] charArr, ref int index)
        {
            List<object> arr = null;
            if (charArr[index++] == '[')
            {
                arr = new List<object>();
                while (index < charArr.Length && charArr[index] != ']')
                {
                    if (charArr[index] == '\"' || (charArr[index] >= '0' && charArr[index] <= '9'))
                    {
                        arr.Add(GetValue(ref charArr, ref index));
                    }
                    else if (charArr[index] == '{')
                    {
                        arr.Add(AnalysisObject(ref charArr, ref index));
                    }
                    else if (charArr[index] == '[')
                    {
                        arr.Add(AnalysisJsonArr(ref charArr, ref index));
                    }
                    else if (charArr[index] == ',')
                    {
                        index++;
                    }
                }
                if (charArr[index] == ']')
                {
                    index++;
                }
                return arr;
            }
            else//异常
            {
                //throw new Exception();
                return null;
            }

        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值