C#中解析JSON数据,并获取到其中的值
1.应需求创建一个json字符串
string json = "[{'name':'张三','age':'20','data':{'id':100,'heigh':'180','weight':'90'}}]";
2.根据字符串的数据结构定义两个类:
public class user
{
public string name{ get; set; }
public string age{ get; set; }
public data data{ get; set; }
}
public class data
{
public int id{ get; set; }
public string heigh{ get; set; }
public string weight{ get; set; }
}
3.再main函数中进行操作,如下main函数:
static void main(string[] args)
{
string json = "[{'name':'张三','age':'20','data':{'id':100,'heigh':'180','weight':'90'}}]";
list<user> jobinfolist = newtonsoft.json.jsonconvert.deserializeobject<list<user>>(json);
foreach (info jobinfo in jobinfolist)
{
console.writeline("username:" + jobinfo.name);
console.writeline("username:" + jobinfo.data.id);
}
}
public class user
{
public string name{ get; set; }
public string age{ get; set; }
public data data{ get; set; }
}
public class data
{
public int id{ get; set; }
public string heigh{ get; set; }
public string weight{ get; set; }
}
下一篇: Idea插件lombok的安装和使用