将JSON转换为C#对象列表时获取空值

时间:2018-06-27 20:58:21

标签: c# arrays json c#-4.0 stripe-payments

这是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数组。

注意:它没有给我任何错误。没有丢失的引用,它完美地通过了这一行代码,但是所有值都保持为空。 在此先感谢大家,我非常感谢。

2 个答案:

答案 0 :(得分:0)

使用[https://app.quicktype.io/#l=cs&r=json2csharp][1]能够为问题中的JSON生成下面的类

using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public partial class Welcome
{
    [JsonProperty("FirstName")]
    public string FirstName { get; set; }

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

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

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

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

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

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

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

    [JsonProperty("BirthYear")]
    [JsonConverter(typeof(ParseStringConverter))]
    public long BirthYear { get; set; }
}

public partial class Welcome
{
    public static Welcome[] FromJson(string json) => JsonConvert.DeserializeObject<Welcome[]>(json, QuickType.Converter.Settings);
}

public static class Serialize
{
    public static string ToJson(this Welcome[] self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
}

internal static class Converter
{
    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
        DateParseHandling = DateParseHandling.None,
        Converters = {
            new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
        },
    };
}

internal class ParseStringConverter : JsonConverter
{
    public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);

    public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null) return null;
        var value = serializer.Deserialize<string>(reader);
        long l;
        if (Int64.TryParse(value, out l))
        {
            return l;
        }
        throw new Exception("Cannot unmarshal type long");
    }

    public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
    {
        if (untypedValue == null)
        {
            serializer.Serialize(writer, null);
            return;
        }
        var value = (long)untypedValue;
        serializer.Serialize(writer, value.ToString());
        return;
    }

    public static readonly ParseStringConverter Singleton = new ParseStringConverter();
}

答案 1 :(得分:0)

将此用作模型类

    using System;
    using System.Collections.Generic;

    using System.Globalization;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;

    public partial class JsonModel
    {
        [JsonProperty("FirstName")]
        public string FirstName { get; set; }

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

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

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

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

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

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

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

        [JsonProperty("BirthYear")]

        public string BirthYear { get; set; }
    }

然后在您的主要课程中完成

            var data = JsonConvert.DeserializeObject<JsonModel>(jsonstring);
            var country = data.Country;
            var birthday = data.BirthDay;