|
|
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
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 获取JObject
|
|
|
/// </summary>
|
|
|
/// <param name="jobj"></param>
|
|
|
/// <param name="prop"></param>
|
|
|
/// <returns></returns>
|
|
|
public static JObject GetJObjectValue(this JObject jobj, string prop)
|
|
|
{
|
|
|
var jt = jobj[prop];
|
|
|
if (jt == null)
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
return jt as JObject;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取JArray
|
|
|
/// </summary>
|
|
|
/// <param name="jobj"></param>
|
|
|
/// <param name="prop"></param>
|
|
|
/// <returns></returns>
|
|
|
public static JArray GetJArrayValue(this JObject jobj, string prop)
|
|
|
{
|
|
|
var jt = jobj[prop];
|
|
|
if (jt == null)
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
return jt as JArray;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取字符串值
|
|
|
/// </summary>
|
|
|
/// <param name="jobj"></param>
|
|
|
/// <param name="prop"></param>
|
|
|
/// <returns></returns>
|
|
|
public static string GetStringValue(this JObject jobj, string prop)
|
|
|
{
|
|
|
var jt = jobj[prop];
|
|
|
if (jt == null)
|
|
|
{
|
|
|
return string.Empty;
|
|
|
}
|
|
|
|
|
|
return jt.ToString();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取int值
|
|
|
/// </summary>
|
|
|
/// <param name="jobj"></param>
|
|
|
/// <param name="prop"></param>
|
|
|
/// <returns></returns>
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取long值
|
|
|
/// </summary>
|
|
|
/// <param name="jobj"></param>
|
|
|
/// <param name="prop"></param>
|
|
|
/// <returns></returns>
|
|
|
public static long GetLongValue(this JObject jobj, string prop)
|
|
|
{
|
|
|
var jt = jobj[prop];
|
|
|
if (jt == null)
|
|
|
{
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
var strVal = jt.ToString();
|
|
|
long rtnVal = 0;
|
|
|
long.TryParse(strVal, out rtnVal);
|
|
|
return rtnVal;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取decimal值
|
|
|
/// </summary>
|
|
|
/// <param name="jobj"></param>
|
|
|
/// <param name="prop"></param>
|
|
|
/// <returns></returns>
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取datetime值
|
|
|
/// </summary>
|
|
|
/// <param name="jobj"></param>
|
|
|
/// <param name="prop"></param>
|
|
|
/// <returns></returns>
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取datetime值,不带秒(yyyy-MM-dd HH:mm)
|
|
|
/// </summary>
|
|
|
/// <param name="jobj"></param>
|
|
|
/// <param name="prop"></param>
|
|
|
/// <returns></returns>
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取int值
|
|
|
/// </summary>
|
|
|
/// <param name="jobj"></param>
|
|
|
/// <param name="prop"></param>
|
|
|
/// <returns></returns>
|
|
|
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;
|
|
|
}
|
|
|
}
|
|
|
}
|