完整的代码如下:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Data data = new Data();
dataid = 1;
datavalue = "cate";
datachild = new List<Data>()
{
new Data(){ id=1,value="cate",child=new List<Data>(){}} ,
new Data(){ id=1,value="cate",child=new List<Data>()
{
new Data()
{
id=2,
value="cate2" ,
child = new List<Data>()
{
new Data()
{
id = 3,
value = "cate3",
child = new List<Data>(){},
}
},
}
}} ,
};
//序列化为json字符串
string json = JsonConvertSerializeObject(data);
ConsoleWriteLine(json);
//反序列化为对象
Data jsonData = JsonConvertDeserializeObject<Data>(json);
}
}
public class Data
{
public int id { get; set; }
public string value { get; set; }
public List<Data> child { get; set; }
}
}
我们验证一下生成的结果:
JObject jo = (JObject)JsonConvertDeserializeObject(json);
ConsoleWriteLine(jo);

再来一个复杂点的JSON结构:
[
{
"downList": [],
"line": {
"Id": -1,
"Name": "admin",
"icCard": "1"
},
"upList": [
{
"endTime": "18:10",
"startTime": "06:40",
"sId": 385,
"sType": "38"
},
{
"endTime": "18:10",
"startTime": "06:40",
"sId": 1036,
"sType": "38"
}
]
},
{
"downList": [],
"line": {
"Id": -1,
"Name": "admin",
"icCard": "1"
},
"upList": [
{
"endTime": "18:10",
"startTime": "06:40",
"sId": 385,
"sType": "38"
},
{
"endTime": "18:10",
"startTime": "06:40",
"sId": 1036,
"sType": "38"
}
]
}
]
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string jsonString = "[{"downList": [],"line": {"Id": -1,"Name": "admin","icCard": "1"},"upList": [{"endTime": "18:10","startTime": "06:40","sId": 385,"sType": "38"},{"endTime": "18:10","startTime": "06:40","sId": 1036,"sType": "38"}]},{"downList": [],"line": {"Id": -1,"Name": "admin","icCard": "1"},"upList": [{"endTime": "18:10","startTime": "06:40","sId": 385,"sType": "38"},{"endTime": "18:10","startTime": "06:40","sId": 1036,"sType": "38"}]}]";
Data[] datas = JsonConvertDeserializeObject<Data[]>(jsonString);
foreach (Data data in datas)
{
downList[] downList = datadownList;
line line = dataline;
upList[] upLists = dataupList;
//输出
ConsoleWriteLine(stringJoin(",", lineId, lineName, lineicCard));
foreach (upList upList in upLists)
{
ConsoleWriteLine(stringJoin(",", upListendTime, upListstartTime, upListsId, upListsType));
}
ConsoleWriteLine("-----------------------------------------------");
}
}
}
public class Data
{
public downList[] downList { get; set; }
public line line { get; set; }
public upList[] upList { get; set; }
}
public class downList
{
}
public class line
{
public int Id { get; set; }
public string Name { get; set; }
public string icCard { get; set; }
}
public class upList
{
public string endTime { get; set; }
public string startTime { get; set; }
public int sId { get; set; }
public string sType { get; set; }
}
}










