【发布时间】:2018-12-06 20:27:23
【问题描述】:
这是json:
[
{
"FirstName": "bob",
"LastName": "ob",
"Country": "vxv",
"CityOrTown": "aaaaa",
"Line1": "3EF1A60C-4en St.dsadsa",
"PostalCode": "91106",
"BirthDay": "07",
"BirthMonth": "06",
"BirthYear": "2000"
},
{
"FirstName": "bbb",
"LastName": "bbb",
"Country": "bbb",
"CityOrTown": "bbb",
"Line1": "bbb",
"PostalCode": "bbb",
"BirthDay": "06",
"BirthMonth": "06",
"BirthYear": "2000"
}
]
这是我想将此 json 转换为的对象:
namespace Stripe
{
public class StripeAccountAdditionalOwner : INestedOptions
{
public StripeAccountAdditionalOwner();
[JsonProperty("[address][city]")]
public string CityOrTown { get; set; }
[JsonProperty("[address][country]")]
public string Country { get; set; }
[JsonProperty("[address][line1]")]
public string Line1 { get; set; }
[JsonProperty("[address][line2]")]
public string Line2 { get; set; }
[JsonProperty("[address][postal_code]")]
public string PostalCode { get; set; }
[JsonProperty("[address][state]")]
public string State { get; set; }
[JsonProperty("[dob][day]")]
public int? BirthDay { get; set; }
[JsonProperty("[dob][month]")]
public int? BirthMonth { get; set; }
[JsonProperty("[dob][year]")]
public int? BirthYear { get; set; }
[JsonProperty("[first_name]")]
public string FirstName { get; set; }
[JsonProperty("[last_name]")]
public string LastName { get; set; }
[JsonProperty("verification[document]")]
public string VerificationDocument { get; set; }
}
}
这是我在控制器中使用的代码:
List<StripeAccountAdditionalOwner> AdditionalOwners = JsonConvert.DeserializeObject<List<StripeAccountAdditionalOwner>>(requestData.CompanyOwners);
requestData.CompanyOwners 是对象的 json 数组。
注意:它没有给我任何错误。没有丢失的引用,它完美地通过了这行代码,但是所有值都保持为空。 在此先感谢各位,非常感谢。
【问题讨论】:
-
查找
JsonProperty属性 (newtonsoft.com/json/help/html/…) 的文档。我认为您误解了该属性的真正作用。 -
是的,您的
[JsonProperty()]属性毫无意义。仅当 json 键与您的属性名称不同时才使用JsonProperty。序列化程序不区分大小写,因此如果您在 JSON 中的属性是someKey并且您的 C# 属性是SomeKey就可以了 -
@user10001850 只需从所有属性中删除
[JsonProperty]属性除非 C# 中的名称与 JSON 中的名称不同。链接的文档 elgonzo 将解释所有这些 -
您显示的 json 不是 Stripe 返回的,您问题中的类在
Stripe.NET内部使用(使用自定义 JsonConverter)将其 json 反序列化为强类型对象。目前还不清楚根本您要达到什么目的:您想反序列化 Stripe 响应还是尝试创建不同的模型(用于不同的目的)? -
然后创建另一个类,该类符合your json。您复制/粘贴的内容不适用于您的情况。
标签: c# arrays json c#-4.0 stripe-payments