old_flsystem/类库/HttpHelper2.1/Helper/JsonHelper.cs

48 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Web.Script.Serialization;
namespace CsharpHttpHelper.Helper
{
/// <summary>
/// Json操作对象 Copyrighthttp://www.httphelper.com/
/// </summary>
internal class JsonHelper
{
/// <summary>
/// 将指定的Json字符串转为指定的T类型对象
/// </summary>
/// <param name="jsonstr">字符串</param>
/// <returns>转换后的对象失败为Null</returns>
internal static object JsonToObject<T>(string jsonstr)
{
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
return javaScriptSerializer.Deserialize<T>(jsonstr);
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 将指定的对象转为Json字符串
/// </summary>
/// <param name="obj">对象</param>
/// <returns>转换后的字符串失败为空字符串</returns>
internal static string ObjectToJson(object obj)
{
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
return javaScriptSerializer.Serialize(obj);
}
catch (Exception)
{
return string.Empty;
}
}
}
}