Json是什么
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它基于JavaScript的一个子集,易于人的编写和阅读,也易于机器解析。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语言。
怎么处理Json
获得类
首先,打开BeJson,把待转换的Json转换为实体类

这里以这个Json为例
json
{
"status": 1,
"statusTxt": "结束",
"code": "00",
"numberCode": 20000,
"msg": "管海反增力员心时了都种两立边直方行加期员进能利战料社组文流同通文因时如事及则又给得子高与文又时强两利",
"msg2": "keh",
"logo": "https://dummyimage.com/100x100",
"percent": 0.82,
"result": {
"pList": [
{
"indexToString": "1",
"index": 1,
"id": 1,
"price": 9.35,
"name": "公司名称1",
"person": "李文武1",
"address": "北京市海淀区西三旗",
"mobile": "11545045576",
"tel": "8874-31809126",
"list": [
{
"auditKey": 1,
"auditValue": 1
},
{
"auditKey": 2,
"auditValue": 1
}
]
},
{
"indexToString": "2",
"index": 2,
"id": 3,
"price": 10.37,
"name": "公司名称2",
"person": "李文武2",
"address": "北京市海淀区西三旗",
"mobile": "13633079592",
"tel": "4320-39851465",
"list": [
{
"auditKey": 1,
"auditValue": 1
},
{
"auditKey": 2,
"auditValue": 1
}
]
}
]
}
}
得到生成的实体类
C#
public class List
{
public int auditKey { get; set; }
public int auditValue { get; set; }
}
public class PList
{
public string indexToString { get; set; }
public int index { get; set; }
public int id { get; set; }
public double price { get; set; }
public string name { get; set; }
public string person { get; set; }
public string address { get; set; }
public string mobile { get; set; }
public string tel { get; set; }
public List<List> list { get; set; }
}
public class Result
{
public List<PList> pList { get; set; }
}
public class Root
{
public int status { get; set; }
public string statusTxt { get; set; }
public string code { get; set; }
public int numberCode { get; set; }
public string msg { get; set; }
public string msg2 { get; set; }
public string logo { get; set; }
public double percent { get; set; }
public Result result { get; set; }
}
引用
将实体添加至项目中:

接下来引用Newtonsoft.Json.dll(自行搜索),用于JSON类库的解析:

在代码中写
C#
IDat.JsonModel.Root res = JsonConvert.DeserializeObject<IDat.JsonModel.Root>(json);
注意
代码中的IDat.JsonModel.Root对应着实体的类名
所得到的对象res中就是Json数据