json字符串如下:
{ success : 0, errorMsg : "错误消息", data : { total : "总记录数", rows : [ { id : "任务ID", workName : "任务名称", assigneeName : "经办人姓名", name : "流程步骤名称", processInstanceInitiatorName : "发起人", processInstanceStartTime : "发起时间", createTime : "到达时间", dueDate : "截止时间" }, { id : "ID", workName : "名称", assigneeName : "经办人", name : "流程", processInstanceInitiatorName : "发起人", processInstanceStartTime : "发起", createTime : "到达", dueDate : "截止" } ] } }
第一步:利用vs2013或者以上版本生成类文件
具体操作:选中json字符串打开vs2013创建一个类文件依次点击:编辑->选择性黏贴->将json粘贴为类如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { //修改成类文件名
public class JsonEntity { public int success { get; set; } public string errorMsg { get; set; } public Data data { get; set; } } public class Data { public string total { get; set; } public Row[] rows { get; set; } } public class Row { public string id { get; set; } public string workName { get; set; } public string assigneeName { get; set; } public string name { get; set; } public string processInstanceInitiatorName { get; set; } public string processInstanceStartTime { get; set; } public string createTime { get; set; } public string dueDate { get; set; } } }
3.Json To Object
添加引用json.net 读取json文件
using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string filePath=@"d:\JsonStr.txt"; string jsonStr=File.ReadAllText(filePath,Encoding.UTF8); JsonEntity je = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonEntity>(jsonStr); Console.WriteLine(je.data.rows[0].name); Console.ReadKey(); } } }