C# 通过JSON 键(key)找值(value)
仅仅支持严格标准的JSON字符串。
{String:String,........}
{"arg":103,"errmsg":""},对于这种arg=103,103不是String,该方法无法获取到。
1 using System.Web.Script.Serialization;
2 using System.Collections.Generic;
3 using System.Web;
4
5 /// <summary>
6 /// 获取JSON格式的值,只支持.NET3.5+
7 /// </summary>
8 /// <param name="input">JSON文本</param>
9 /// <param name="getkey">已知的KEY</param>
10 /// <returns>获取已知KEY的值</returns>
11 internal static String getJsonval(String input, String key)
12 {
13 if (String.IsNullOrEmpty(input) || String.IsNullOrEmpty(key)) return String.Empty;
14 JavaScriptSerializer serializer = new JavaScriptSerializer();
15 Dictionary<string, object> json = serializer.DeserializeObject(input) as Dictionary<string, object>;
16 object value;
17 json.TryGetValue(key, out value);
18 return value as String;
19 }