using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Newtonsoft.Json;
namespace Quiz.Utility
{
public class JsonHelper
{
///
/// 解析JSON字符串生成对象实体
///
/// 对象类型
/// json字符串(eg.{"ID":"112","Name":"石子儿"})
/// 对象实体
public static T ParseFromJson(string json) where T : class
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
T t = o as T;
return t;
}
///
/// 将对象序列化为JSON格式
///
/// 对象
/// json字符串
public static string SerializeObject(object o)
{
string json = JsonConvert.SerializeObject(o);
return json;
}
}
}