【问题标题】:Read JSON file and map to C# objects读取 JSON 文件并映射到 C# 对象
【发布时间】:2016-05-13 09:38:39
【问题描述】:

我想读取一个 JSON 文件并将其映射到类对象。我将如何在 C# 中做到这一点?

JSON

{  
   "companyName":"Test company",
   "companyNumber":"1234",
   "address":{  
      "buildingNumber":"33",
      "street":"Caledon Road",
      "county":"Barking and Dagenham",
      "postalTown":"Essex",
      "postcode":"E62HE"
   }
}

C#代码

public class CompanyInfo
{ 
    public string companyName{ get;set;}
    public string companyNumber{ get;set;}
    public string buildingNumber{ get;set;}
    public string street{ get;set;}
    public string county{ get;set;}
    public string postalTown{ get;set;}
    public string postCode{ get;set;}
}

【问题讨论】:

标签: c# json


【解决方案1】:

制作你的代码

    var json = {  
                 "companyName":"Test company",
                 "companyNumber":"1234",
                 "address":{  
                     "buildingNumber":"33",
                     "street":"Caledon Road",
                     "county":"Barking and Dagenham",
                     "postalTown":"Essex",
                     "postcode":"E62HE"
                  }
               }

 public class CompanyInfo
    { 
    public string companyName{ get;set;}
    public string companyNumber{ get;set;}
    public Address address {get;set;}
    }

 public class Address
   {
    public string buildingNumber{ get;set;}
    public string street{ get;set;}
    public string county{ get;set;}
    public string postalTown{ get;set;}
    public string postCode{ get;set;}
   }

然后使用Newtonsoft.Json反序列化json

var results = JsonConvert.DeserializeObject<CompanyInfo>(json);

【讨论】:

  • buildingNumber 应该在地址内
【解决方案2】:

首先创建一个类来匹配JSON,这可以使用这个超级好用的工具json2csharp.com轻松完成

您提供的 JSON 转换为此

public class Address
    {
        public string buildingNumber { get; set; }
        public string street { get; set; }
        public string county { get; set; }
        public string postalTown { get; set; }
        public string postcode { get; set; }
    }

    public class RootObject
    {
        public string companyName { get; set; }
        public string companyNumber { get; set; }
        public Address address { get; set; }
    }

然后将您的 JSON 反序列化为您刚刚使用 JSON.net (nuget Install-Package Newtonsoft.Json) 定义的类型的对象

public void LoadJson()
{
    using (StreamReader r = new StreamReader("file.json"))
    {
        string json = r.ReadToEnd();
        RootObject company = JsonConvert.DeserializeObject<RootObject>(json);
   }
}

我建议将 RootObject 重命名为在您的应用程序中更有意义的名称

【讨论】:

  • 感谢 json2csharp.com 的建议!
  • 第二。多年后,解决了我的问题:)
【解决方案3】:

您需要两个类 - CompanyInfoAddressCompanyInfo 必须包含 Address 对象,因为 json 在 companyInfo 中有地址对象:

public class CompanyInfo
{ 
    public string companyName{ get;set;}
    public string companyNumber{ get;set;}
    public Address address{get;set;}
}

public class Address
{
    public string buildingNumber{ get;set;}
    public string street{ get;set;}
    public string county{ get;set;}
    public string postalTown{ get;set;}
    public string postCode{ get;set;}
}

那么你应该使用Newtonsoft.Json NuGet Package 或其他东西反序列化 json。

【讨论】:

    【解决方案4】:

    你必须使用 JSON.Net enter link description here

    这篇文章也很有趣:How can I parse JSON with C#?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 2022-11-27
      • 2021-02-08
      • 2012-07-12
      • 1970-01-01
      • 2012-04-16
      相关资源
      最近更新 更多