public class JsonHelper
{
///
/// 将对象序列化为JSON格式
///
/// 对象
///
public static string SerializeObject(object o)
{
string json = JsonConvert.SerializeObject(o);
return json;
}
///
/// 解析JSON字符串生成对象实体
///
///
/// json字符串(eg.{"ID":"112","Name":"石子儿"})
///
public static T DeserializeJsonToObject
{
JsonSerializer serializer = new JsonSerializer();
//serializer.NullValueHandling = NullValueHandling.Ignore;
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
T t = o as T;
return t;
}
///
/// 解析JSON数组生成对象实体集合
///
///
/// 【JsonHelper】json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])
///
public static List
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List
List
return list;
}
///
/// 反序列化JSON到给定的匿名对象.
///
///
/// json字符串
/// 匿名对象
///
public static T DeserializeAnonymousType
{
T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject);
return t;
}
}
推荐阅读
- 关于欧几里得算法和拓展欧几里得算法
- 蛇精,蜈蚣
- 程序|android 不销毁当前activity,返回上一层,亲测有效
- c语言while循环打印等腰三角型
- 电子DIY|《电子DIY》之《单片机实践项目》之基于51单片机的TCS230颜色识别模块
- Jmysy-Pear-Admin-Layui
- 输出HTML源码进行压缩
- 获取游览器类型
- 框架|Java如何使用Web3j开发以太坊区块链Dapp的教程