【问题标题】:JSON with array structure [duplicate]具有数组结构的 JSON [重复]
【发布时间】:2020-03-19 04:21:46
【问题描述】:

我只是想问一下如何在c#中实现一个带有数组结构的json数据。这是下面的示例数据:

{
 "contact":
  {
    "contact_type_ids": ["CUSTOMER"],
     "name":"JSON Sample ResellerAVP",
     "main_address":
     {
        "address_type_id":"ACCOUNTS",
         "address_line_1":"Ayala Hills",
         "city":"Muntinlupa",
         "region":"NCR",
         "postal_code":"1770",
         "country_group_id":"ALL"
     }
  }
}

【问题讨论】:

  • json2csharp.com 根据给定的 json 输入生成 c# 类。使用序列化/反序列化来填充类。
  • Visual Studio 有这个工具。复制你的字符串 json,去 Edit->Paste Special->Paste Json as classes
  • 哦,谢谢大家。我会试试你的建议。 :)

标签: c# arrays json


【解决方案1】:

JSON 概览:

  • { } - 对象
  • [ ] - 数组
  • "a": something - 财产

属性可以将对象、数组或值类型作为其值。例如:

{
    "a": true,
    "b": "hello",
    "c": 5.2,
    "d": 1,
    "e": { "eChildProperty": "test" },
    "f": [ "a", "b", "c" ]
}

让我们开始将这个 JSON 转录成类吧!

{
 "contact":
  {
    "contact_type_ids": ["CUSTOMER"],
     "name":"JSON Sample ResellerAVP",
     "main_address":
     {
        "address_type_id":"ACCOUNTS",
         "address_line_1":"Ayala Hills",
         "city":"Muntinlupa",
         "region":"NCR",
         "postal_code":"1770",
         "country_group_id":"ALL"
     }
  }
}

好的,所以我们有一个带有属性“contact”的根对象,它也是一个对象。让我们代表这两个:

public class RootObject
{
    public Contact Contact { get; set; }
}

public class Contact
{

}

现在我们需要添加联系人的属性。它有3个:contact_type_ids是一个字符串数组,name是一个字符串,主地址是一个复杂对象。让我们代表那些:

public class Contact
{
    [JsonProperty("contact_type_ids")]
    public IList<string> ContactTypeIds { get; set; } // I'm using an IList, but any collection type or interface should work

    public string Name { get; set; }

    [JsonProperty("main_address")]
    public Address MainAddress { get; set; }
}

public class Address
{

}

最后我们需要处理 Address 对象:

public class Address
{
    [JsonProperty("address_type_id")]
    public string AddressTypeId { get; set; }

    [JsonProperty("address_line_1")]
    public string AddressLine1 { get; set; }

    public string City { get; set; }

    public string Region { get; set; }

    [JsonProperty("postal_code")]
    public string PostalCode { get; set; }

    [JsonProperty("country_group_id")]
    public string CountryGroupId { get; set; }
}

把这一切放在一起,我们得到:

public class RootObject
{
    public Contact Contact { get; set; }
}

public class Contact
{
    [JsonProperty("contact_type_ids")]
    public IList<string> ContactTypeIds { get; set; } // I'm using an IList, but any collection type or interface should work

    public string Name { get; set; }

    [JsonProperty("main_address")]
    public Address MainAddress { get; set; }
}

public class Address
{
    [JsonProperty("address_type_id")]
    public string AddressTypeId { get; set; }

    [JsonProperty("address_line_1")]
    public string AddressLine1 { get; set; }

    public string City { get; set; }

    public string Region { get; set; }

    [JsonProperty("postal_code")]
    public string PostalCode { get; set; }

    [JsonProperty("country_group_id")]
    public string CountryGroupId { get; set; }
}

我们可以这样使用它:

RootObject deserialized = JsonConvert.DeserializeObject<RootObject>(jsonString);

Try it online


JSON 并不复杂。只需查看您拥有的数据即可理解并手动转换为类非常简单。

【讨论】:

  • 谢谢约翰。我也试试这个。 :)
  • @Developer 请尝试了解我们如何从 JSON 到 C# 类或从 C# 类到 JSON。它可能对您的帮助远不止简单地了解如何使用工具将 JSON 转换为类,尤其是因为这些工具并不总是正确或不选择正确的 C# 类型。
猜你喜欢
  • 2012-01-22
  • 2020-02-27
  • 2013-03-05
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
  • 2017-04-16
  • 2020-04-25
相关资源
最近更新 更多