在校时不知道怎么转json格式和解析json格式,然后经过自己的一番研究(找资料)终于完成了,哈哈,有点小开心,希望这些代码能帮助下刚入门的新手(本人第一次写博客书写方面有些不规范,希望各位勿喷,建议是可以提的,本人虚心接受,在这谢谢各位小哥哥小姐姐了)。
转简单的json格式,代码如下:
List<dictionlist> list = new List<dictionlist>();
dictionlist dict = new dictionlist();
dict.id = 1;
dict.name = "曾";
dict.dic = "研发";
dict.dict = "制作";
list.Add(dict);
dictionlist dict2 = new dictionlist();
dict2.id = 2;
dict2.name = "曾2";
dict2.dic = "研发2";
dict2.dict = "制作2";
list.Add(dict2);
json = JsonConvert.SerializeObject(list);
效果图:
解析简单的json格式,代码如下:
JArray str = (JArray)JsonConvert.DeserializeObject(json);
foreach (var item in str)
{
int id = Convert.ToInt32(item["id"]);
string name = item["name"].ToString();
string dic = item["dic"].ToString();
string dict = item["dict"].ToString();
}
效果图:
转两次json格式,代码如下:
List<dictionlist> list = new List<dictionlist>();
dictionlist dict = new dictionlist();
dict.id = 1;
dict.name = "曾";
dict.dic = "研发";
dict.dict = "制作";
list.Add(dict);
dictionlist dict2 = new dictionlist();
dict2.id = 2;
dict2.name = "曾2";
dict2.dic = "研发2";
dict2.dict = "制作2";
list.Add(dict2);
string json2 = JsonConvert.SerializeObject(list);
List<schoollist> listschool = new List<schoollist>();
schoollist school = new schoollist();
school.schoolid = 1;
school.schooldata = json2;
listschool.Add(school);
json = JsonConvert.SerializeObject(listschool);
效果图:
解析两次json格式,代码如下:
JArray array = (JArray)JsonConvert.DeserializeObject(json);
foreach (var item2 in array)
{
int schoolid = Convert.ToInt32(item2["schoolid"]);
JArray array2 = (JArray)JsonConvert.DeserializeObject(item2["schooldata"].ToString());
foreach (var item in array2)
{
int id = Convert.ToInt32(item["id"]);
string name = item["name"].ToString();
string dic = item["dic"].ToString();
string dict = item["dict"].ToString();
}
}
效果图: