You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Core/Extension/JsonExtension.cs

154 lines
3.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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>
/// 获取字符串值
/// </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>
/// 获取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;
}
}
}