c#读取json中指定Key的Value

最近项目中有很多调用Api的地方,本来是打算写实体类把返回的JSON转实体类,但接口太多,好多还是不常用的就自己写了一个查找JSON中指定Key的Value。代码仅供参考,只是一个草版,希望大家多给给意见。

#region 传入指定json字符串和key值查找传入key的value(object)
        public static object digui(string json, string key)
        {
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            Dictionary<string, object> js = javaScriptSerializer.DeserializeObject(json) as Dictionary<string, object>;
            object value = null;
            foreach (KeyValuePair<string, object> item in js)
            {
                if (item.Key == key)
                {
                    value = item.Value;
                    break;
                }
                else if (item.Value is Dictionary<string, object>)
                {
                    object se = di(item.Value as Dictionary<string, object>, key);
                    if (se != null)
                    {
                        value = se;
                        break;
                    }
                }
                else if (item.Value is object[])
                {
                    object[] se = item.Value as object[];
                    if (se.Length > 0)
                    {
                        foreach (object o in se)
                        {
                            if (o is Dictionary<string, object>)
                            {
                                object ses = di(item.Value as Dictionary<string, object>, key);
                                if (ses != null)
                                {
                                    value = ses;
                                    break;
                                }
                            }
                        }
                    }
                }


            }
            return value;

        }
        private static object di(Dictionary<string, object> dic, string key)
        {
            object vale = null;
            foreach (KeyValuePair<string, object> item in dic)
            {
                if (item.Key == key)
                {
                    vale = item.Value;
                    break;
                }
                else if (item.Value is Dictionary<string, object>)
                {
                    object fan = di(item.Value as Dictionary<string, object>, key);
                    if (fan != null)
                    {
                        vale = fan;
                        break;
                    }
                }
                else if (item.Value is object[])
                {
                    object[] se = item.Value as object[];
                    if (se.Length > 0)
                    {
                        foreach (object o in se)
                        {
                            if (o is Dictionary<string, object>)
                            {
                                object ses = di(o as Dictionary<string, object>, key);
                                if (ses != null)
                                {
                                    vale = ses;
                                    break;
                                }
                            }
                        }
                    }
                }

            }
            return vale;
        }
        #endregion

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要替换Json数据指定key的值,可以使用Json.NET库或者System.Text.Json的方法,以下是使用Json.NET库的示例: ```csharp using Newtonsoft.Json.Linq; // 假设原始的Json数据为 var jsonString = "{\"name\":\"Alice\",\"age\":25}"; // 将Json字符串转换为JObject对象 var jObject = JObject.Parse(jsonString); // 替换指定key的值 jObject["age"] = 30; // 将JObject对象转换为Json字符串 var newJsonString = jObject.ToString(); ``` 在上面的代码,首先将原始的Json字符串转换为JObject对象,然后使用方括号运算符[]来访问指定key,并将其值替换为新的值,最后将JObject对象转换为Json字符串。 如果使用System.Text.Json库,可以使用JsonDocument类来访问Json数据,并使用JsonElement类型的值来修改指定key的值,以下是一个示例: ```csharp using System.Text.Json; // 假设原始的Json数据为 var jsonString = "{\"name\":\"Alice\",\"age\":25}"; // 将Json字符串转换为JsonDocument对象 var jsonDocument = JsonDocument.Parse(jsonString); // 获取根元素 var rootElement = jsonDocument.RootElement; // 替换指定key的值 rootElement.GetProperty("age").SetInt32(30); // 将JsonDocument对象转换为Json字符串 var newJsonString = jsonDocument.RootElement.GetRawText(); ``` 在上面的代码,首先将原始的Json字符串转换为JsonDocument对象,然后使用GetProperty方法获取指定key,并使用SetInt32方法将其值替换为新的值,最后将JsonDocument对象转换为Json字符串。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值