using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Myshipping.Core { public static class JsonExtension { /// /// 获取JObject /// /// /// /// public static JObject GetJObjectValue(this JObject jobj, string prop) { var jt = jobj[prop]; if (jt == null) { return null; } return jt as JObject; } /// /// 获取字符串值 /// /// /// /// public static string GetStringValue(this JObject jobj, string prop) { var jt = jobj[prop]; if (jt == null) { return string.Empty; } return jt.ToString(); } /// /// 获取int值 /// /// /// /// public static int GetIntValue(this JObject jobj, string prop) { var jt = jobj[prop]; if (jt == null) { return 0; } var strVal = jt.ToString(); int rtnVal = 0; int.TryParse(strVal, out rtnVal); return rtnVal; } /// /// 获取decimal值 /// /// /// /// public static decimal GetDecimalValue(this JObject jobj, string prop) { var jt = jobj[prop]; if (jt == null) { return 0; } var strVal = jt.ToString(); decimal rtnVal = 0; decimal.TryParse(strVal, out rtnVal); return rtnVal; } /// /// 获取datetime值 /// /// /// /// public static DateTime? GetDateTimeValue(this JObject jobj, string prop) { var jt = jobj[prop]; if (jt == null) { return null; } var strVal = jt.ToString(); DateTime rtnVal = DateTime.MinValue; if (DateTime.TryParse(strVal, out rtnVal)) { return rtnVal; } return null; } /// /// 获取datetime值,不带秒(yyyy-MM-dd HH:mm) /// /// /// /// public static DateTime? GetDateTimeMinuteValue(this JObject jobj, string prop) { var jt = jobj[prop]; if (jt == null) { return null; } var strVal = jt.ToString() + ":00"; DateTime rtnVal = DateTime.MinValue; if (DateTime.TryParse(strVal, out rtnVal)) { return rtnVal; } return null; } /// /// 获取int值 /// /// /// /// public static bool GetBooleanValue(this JObject jobj, string prop) { var jt = jobj[prop]; if (jt == null) { return false; } var strVal = jt.ToString(); bool.TryParse(strVal, out bool rtnVal); return rtnVal; } } }