【C#教程】解析JSON

Json是什么

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

[v_notice]作者:若梦儿
链接:https://www.jianshu.com/p/1970a209672f
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。[/v_notice]

怎么处理Json

  • 首先,打开BeJson,把待转换的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
          }
        ]
      }
    ]
  }
}

得到生成的实体

public class List
{
    /// <summary>
    /// 
    /// </summary>
    public int auditKey { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public int auditValue { get; set; }
}

public class PList
{
    /// <summary>
    /// 
    /// </summary>
    public string indexToString { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public int index { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public int id { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public double price { get; set; }
    /// <summary>
    /// 公司名称1
    /// </summary>
    public string name { get; set; }
    /// <summary>
    /// 李文武1
    /// </summary>
    public string person { get; set; }
    /// <summary>
    /// 北京市海淀区西三旗
    /// </summary>
    public string address { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string mobile { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string tel { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public List<List> list { get; set; }
}

public class Result
{
    /// <summary>
    /// 
    /// </summary>
    public List<PList> pList { get; set; }
}

public class Root
{
    /// <summary>
    /// 
    /// </summary>
    public int status { get; set; }
    /// <summary>
    /// 结束
    /// </summary>
    public string statusTxt { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string code { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public int numberCode { get; set; }
    /// <summary>
    /// 管海反增力员心时了都种两立边直方行加期员进能利战料社组文流同通文因时如事及则又给得子高与文又时强两利
    /// </summary>
    public string msg { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string msg2 { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public string logo { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public double percent { get; set; }
    /// <summary>
    /// 
    /// </summary>
    public Result result { get; set; }
}
  • 将实体添加至项目中                                                                                                                                                                                                                                                               
  • 下载Json解析DLL
    [dltable file="Newtonsoft.Json.dll" size="325.50KB"]COS下载[/dltable]
  • 添加引用                                                                                                                                                                                                                                                                                             
  • 在代码中写
IDat.JsonModel.Root res = JsonConvert.DeserializeObject<IDat.JsonModel.Root>(json);

[v_blue]注意:IDat.JsonModel.Root是实体的类名[/v_blue]

  • 对象res中就是Json数据

测试

稍后贴出

声明:本文为原创,作者为 admin,转载时请保留本声明及附带文章链接:https://cszj.wang/archives/1165.html