JSONNewtonsoft使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; namespace Newtonsoft使用 { class Program { static void Main(string[] args) { #region 全局属性设置 Newtonsoft.Json.JsonSerializerSettings setting = new Newtonsoft.Json.JsonSerializerSettings(); JsonConvert.DefaultSettings = new Func<JsonSerializerSettings>(() => { //日期类型默认格式化处理 setting.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat; setting.DateFormatString = "yyyy-MM-dd HH:mm:ss"; //空值处理 setting.NullValueHandling = NullValueHandling.Ignore; return setting; }); #endregion IsoDateTimeConverter timeFormat = new IsoDateTimeConverter(); timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; #region 序列化加日期hi格式化 Studer studer = new Studer() { Age=18.ToString(), IsOpen=true, Name="张三" , Time=DateTime.Now}; string json=Newtonsoft.Json.JsonConvert.SerializeObject(studer); //反序列化 object obs = JObject.Parse(json); #endregion #region 反序列化 //反序列化成匿名对象 var stu = JsonConvert.DeserializeObject(json); //根据泛型反序列化 var stu1=JsonConvert.DeserializeObject<Studer>(json).Name; #endregion #region 匿名对象解析 var obj = new { ID = 2, Name = "李四" }; string objStr=JsonConvert.SerializeObject(obj); //把匿名json转换成对象 object obj2=JsonConvert.DeserializeAnonymousType(objStr, obj); #endregion Console.ReadKey();
把json数组字符串解析出对象
// <summary>
/// 批量删除供应商类别
/// </summary>
/// <returns></returns>
public ActionResult DeleteAll(string ids)
{
dynamic dynamic = JToken.Parse(ids) as dynamic;
foreach (var item in dynamic)
{
var ix=(string)item.id;
}
return View();
}
} } public class Studer { public string Name { get; set; } public string Age { get; set; } public bool IsOpen { get; set; } public DateTime Time { get; set; } } }