欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 关于使用JavaScriptSerializer 序列化与返序列化的操作

程序员文章站 2023-08-12 11:09:25
//开始解析 //引用 //using System.Web.Script.Serialization; JavaScriptSerializer js = new JavaScriptSerializer(); Dictionary oList = js.Deser ......

//开始解析 

//引用

//using system.web.script.serialization;

javascriptserializer js = new javascriptserializer();
dictionary<string, object> olist = js.deserializeobject("json字符串") as dictionary<string, object>;
if (olist != null)
{
string strret = "";
if (olist.containskey("ret"))
{
strret = olist["ret"].tostring();
}
if (strret == "0")
{

dictionary<string, object> odatalist = olist["data"] as dictionary<string, object>;
object[] oitem_listdata = odatalist["item_list"] as object[];

if (oitem_listdata != null)
{
system.linq.iorderedenumerable<object> q = from a in oitem_listdata orderby ((dictionary<string, object>)a)["字段"].tostring() descending select a;//排序的

foreach (var oobj in q)
{
dictionary<string, object> ocountriesdatalist = oobj as dictionary<string, object>;

//匹配到json串的内容 同过实体依次赋值

if (ocountriesdatalist .containskey("类型"))
{
类型= ocountriesdatalist ["类型"].tostring();
}

if (ocountriesdatalist .containskey("内容"))
{
内容= ocountriesdatalist ["内容"].tostring();
}

//如果 内容里面  还有下一级别的话 则继续往下面解析 json

object[] datelist = ocountriesdatalist ["内容"] as object[];
if (datelist != null)
{
system.linq.iorderedenumerable<object> qp = from a in datelist  orderby ((dictionary<string, object>)a)["时间"].tostring() ascending select a;//时间排序

foreach (var oobj1 in qp)
{

dictionary<string, object> lowesdatalist = oobj1 as dictionary<string, object>;//数据集合 

//得到里面 对用的 内容

datetime date = datetime.parse(lowesdatalist ["时间"].tostring()); //日期

 

//你的类 继续赋值 add

}
}
}
}