【C#基础】 读取json某个键值
/// <summary> /// 用JSObject的方法获取json指定第一层某个键的值 /// </summary> /// <param name="jsontext">json内容</param> /// <param name="key">键名</param> /// <param name="value">键值</param> /// <returns>是否获取成功</returns> public static bool GetJsonValue(string jsontext, string key, out string value) { bool IsGetSuccess = false; value = ""; try { if (key.Contains("|")) { string[] keys = key.Split('|'); JSObject jsonObject = (JSObject)EvalJsonObject(jsontext); for (int i = 0; i < keys.Length; i++) { if (i == keys.Length - 1) { value = jsonObject[keys[i]].ToString(); } else { jsonObject = (JSObject)jsonObject[keys[i]]; } } if (string.IsNullOrEmpty(value)) { IsGetSuccess = false; } else { IsGetSuccess = true; } } else { JSObject jsonObject = (JSObject)EvalJsonObject(jsontext); value = jsonObject[key].ToString(); if (string.IsNullOrEmpty(value)) { IsGetSuccess = false; } else { IsGetSuccess = true; } } } catch (Exception ex) { IsGetSuccess = false; } return IsGetSuccess; }