在C#后台,这种JSON数据要怎么获取里面的所有参数内容呢?
{
"result":"success",
"root":{
"track":[
{
"orderno":"A210600870",
"billcode":"400039547",
"detail":[
{
"memo":"快件已发往深圳",
"time":"2022-06-09 13:14:00",
"scantype":"发往"
},
{
"memo":"联运通 已收运(10件)",
"time":"2022-06-09 13:12:35",
"scantype":"开单"
},
{
"memo":"快件到达深圳中转中心",
"time":"2022-06-09 13:14:00",
"scantype":"到达"
},
{
"memo":"快件开始派送",
"time":"2022-06-09 13:15:00",
"scantype":"派送"
},
{
"memo":"蔡杰杰已签收已签收",
"time":"2022-06-09 13:15:00",
"scantype":"签收"
}
]
}
]
},
"message":""
}
想通过实体类来获取里面的内容。谢谢!!!
按照这个结构建实体类,然后用
Newtonsoft.Json 反序列化 就可以了
public class Result
{
public string result { get; set; }
public RootClass root { get; set; }
}
public class RootClass
{
public List<TrackClass> track { get; set; }
}
public class TrackClass
{
public string orderno { get; set; }
public string billcode { get; set; }
public List<Detail> detail { get; set; }
}
public class Detail
{
public string memo { get; set; }
public string time { get; set; }
public string scantype { get; set; }
}
序列化代码
var model = Newtonsoft.Json.JsonConvert.DeserializeObject<Result>(jsonStr);
结构确定,然后是操作
先定义里面的小结构,就不应该是不定长度的类型,字符二维数组detail内部结构
detail再定义为数组或链表,链表节省空间,速度慢
然后再定义大结构
数据结构定义好就简单了