public class ExectendHelp
{
private int index = 0;
public void GetLast(JObject obj, ref JToken token, string key)
{
JToken t = null;
string[] arr = key.Split('.');
int length = arr.Length;
if (index != length)
{
string strArr = arr[index++];
if (strArr.IndexOf('[') >= 0)
{
string[] strArrItem = strArr.Replace("]", "").Split('[');
if (index == arr.Length)
{
token = obj[strArrItem[0]][Convert.ToInt32(strArrItem[1])];
}
else
{
t = obj[strArrItem[0]][Convert.ToInt32(strArrItem[1])];
JObject jj = (JObject)t;
GetLast(jj, ref token, key);
}
}
else
{
if (index == arr.Length)
{
token = obj[strArr];
}
else
{
t = obj[strArr];
JObject jj = (JObject)t;
GetLast(jj, ref token, key);
}
}
}
}
}
/// <summary>
/// 反序列化Json得到指定键名的值
/// </summary>
/// <param name="json">操作的Json字符串</param>
/// <param name="key">键名</param>
/// <returns></returns>
public static object GetValueForJson(this string json, string key)
{
int length = key.Split('.').Length;
object obj = null;
if (length == 0)
{
obj = null;
}
else
{
dynamic DynamicObject = JsonConvert.DeserializeObject<dynamic>(json);
JObject array = DynamicObject;
JToken token = null;
ExectendHelp ExectendHelp = new ExectendHelp();
ExectendHelp.GetLast(array, ref token, key);
obj = token.Value<object>();
}
return obj;
}
private static T GetValueForJson<T>(this string json, string key)
{
int length = key.Split('.').Length;
T obj = default(T);
if (length != 0)
{
dynamic DynamicObject = JsonConvert.DeserializeObject<dynamic>(json);
JObject array = DynamicObject;
JToken token = null;
ExectendHelp ExectendHelp = new ExectendHelp();
ExectendHelp.GetLast(array, ref token, key);
obj = token.Value<T>();
}
return obj;
}
public static string UpdateValueForJson(this string json, string key, object obj)
{
return string.Empty;
}
/// <summary>
/// 得到数组
/// </summary>
/// <param name="json">操作的Json字符串</param>
/// <param name="key">键名</param>
/// <returns></returns>
public static Array GetArray(this string json, string key)
{
Array arr = GetValueForJson<Array>(json, key);
return arr;
}
/// <summary>
/// 得到String数组
/// </summary>
/// <param name="json">操作的Json字符串</param>
/// <param name="key">键名</param>
/// <returns></returns>
public static string[] GetStringArray(this string json, string key)
{
string[] strArr = GetValueForJson<string[]>(json, key);
return strArr;
}
/// <summary>
/// 得到String数组
/// </summary>
/// <param name="json">操作的Json字符串</param>
/// <param name="key">键名</param>
/// <returns></returns>
public static int[] GetIntArray(this string json, string key)
{
int[] intArr = GetValueForJson<int[]>(json, key);
return intArr;
}
/// <summary>
/// 得到Sting字符串
/// </summary>
/// <param name="json">操作的Json字符串</param>
/// <param name="key">键名</param>
/// <returns></returns>
public static string GetString(this string json, string key)
{
string str = GetValueForJson<string>(json, key);
return str;
}
/// <summary>
/// 得到数字
/// </summary>
/// <param name="json">操作的Json字符串</param>
/// <param name="key">键名</param>
/// <returns></returns>
public static int GetInt(this string json, string key)
{
int i = GetValueForJson<int>(json, key);
return i;
}
/// <summary>
/// 得到时间
/// </summary>
/// <param name="json">操作的Json字符串</param>
/// <param name="key">键名</param>
/// <returns></returns>
public static DateTime GetDateTime(this string json, string key)
{
DateTime DateTime = GetValueForJson<DateTime>(json, key);
return DateTime;
}