读取JSON文件并映射到C#对象

时间:2016-05-13 09:38:39

标签: c# json

我想读取一个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;}
}

4 个答案:

答案 0 :(得分:4)

制作代码

    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);

答案 1 :(得分:4)

首先创建一个匹配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重命名为应用程序中更有意义的东西

答案 2 :(得分:2)

您需要两个课程 - 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。

答案 3 :(得分:0)

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

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