求 C# json 解析类 有数组应该怎样解析出来呢?
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/15 10:52:46
求 C# json 解析类 有数组应该怎样解析出来呢?
求 C# json 解析类
有数组应该怎样解析出来呢?
求 C# json 解析类 有数组应该怎样解析出来呢?
/// <summary>
/// 将对象转化城Json
/// </summary>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string ToJson(object obj)
{
string output = string.Empty;
DataContractJsonSerializer dcJson = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream ms = new MemoryStream())
{
dcJson.WriteObject(ms, obj);
output = Encoding.UTF8.GetString(ms.ToArray());
}
return output;
}
/// <summary>
/// 将Json字符串转化成对象
/// </summary>
/// <param name="type">对象的类型</param>
/// <param name="output">Json字符串</param>
/// <returns></returns>
public static object ToClass(Type type, string output)
{
object result;
DataContractJsonSerializer outDs = new DataContractJsonSerializer(type);
using (MemoryStream outMs = new MemoryStream(Encoding.UTF8.GetBytes(output)))
{
result = outDs.ReadObject(outMs);
}
return result;
}