简体   繁体   English

C# 反序列化具有多个对象的 JSON 数组

[英]C# Deserialize JSON array with multiple objects

I'm taking in a JSON that I do not control in the format of this sample:我正在接受一个我无法控制的 JSON 格式:

{
  "Transaction Information": [
    {
      "Type": "This is the Type"
    },
    {
      "Action": "No"
    },
    {
      "Owner": "Simpsons"
    },
    {
      "Buyer/Broker": "Y"
    },
    {
      "Compensation to Buyer": 3.0
    }
  ]
}

I want to deserialize it to a class such as:我想将它反序列化为一个类,例如:

public class Transaction
{
    [JsonProperty("Transaction Information")]
    public TransactionInformation[] TransactionInformation { get; set; }
}

public partial class TransactionInformation
{
    [JsonProperty("Type", NullValueHandling = NullValueHandling.Ignore)]
    public string Type { get; set; }

    [JsonProperty("Action", NullValueHandling = NullValueHandling.Ignore)]
    public string Action { get; set; }

    [JsonProperty("Owner", NullValueHandling = NullValueHandling.Ignore)]
    public string Owner { get; set; }

    [JsonProperty("Buyer/Broker", NullValueHandling = NullValueHandling.Ignore)]
    public string BuyerBroker { get; set; }

    [JsonProperty("Compensation to Buyer", NullValueHandling = NullValueHandling.Ignore)]
    public long? CompensationToBuyer { get; set; }
}

using the code使用代码

var obj = JsonConvert.DeserializeObject<Transaction>(json);

However that gives me a Transaction.TransactionInformation object with 5 records each with all 5 elements with each record having all null values except for one of the 5 elements.但是,这给了我一个 Transaction.TransactionInformation 对象,其中包含 5 个记录,每个记录包含所有 5 个元素,每个记录都具有除 5 个元素之一之外的所有空值。

在此处输入图片说明

Is there a simple way to to return all 5 elements in one record?有没有一种简单的方法可以在一个记录中返回所有 5 个元素?

Is there a simple way to to return all 5 elements in one record?有没有一种简单的方法可以在一个记录中返回所有 5 个元素?

Sure -- Just put each property in a single record:当然 - 只需将每个属性放在一个记录中:

var finalRecord = new TransactionInformation
{
    Type = obj.TransactionInformation.FirstOrDefault(x => !string.IsNullOrEmpty(x.Type))?.Type,
    Action = obj.TransactionInformation.FirstOrDefault(x => !string.IsNullOrEmpty(x.Action))?.Action,
    Owner = obj.TransactionInformation.FirstOrDefault(x => !string.IsNullOrEmpty(x.Owner))?.Owner,
    BuyerBroker = obj.TransactionInformation.FirstOrDefault(x => !string.IsNullOrEmpty(x.BuyerBroker))?.BuyerBroker,
    CompensationToBuyer = obj.TransactionInformation.FirstOrDefault(x => x.CompensationToBuyer.HasValue)?.CompensationToBuyer
};

That JSON data you are working with isn't in the most convenient format.您正在使用的 JSON 数据不是最方便的格式。 In a perfect world it would look like this:在一个完美的世界中,它看起来像这样:

{
    "Transaction Information": [{
            "Type": "This is the Type",
            "Action": "No",
            "Owner": "Simpsons",
            "Buyer/Broker": "Y",
            "Compensation to Buyer": 3.0
        }
    ]
}

Then what you were doing would have worked fine and you wouldn't have to do this last step to normalize the data.那么你正在做的事情会很好地工作,你不必做这最后一步来规范化数据。

You can create a custom JsonConverter that enables you to hook into the deserialization for the type:您可以创建一个自定义的 JsonConverter,使您能够连接到类型的反序列化:

public class TransactionConverter : JsonConverter<Transaction>
{
    public override void WriteJson(JsonWriter writer, Transaction value, JsonSerializer serializer)
    {
    
    }

    public override Transaction ReadJson(JsonReader reader, Type objectType, Transaction existingValue,
        bool hasExistingValue, JsonSerializer serializer)
    {
    
        var rootChildren = JToken.ReadFrom(reader).First.Children();
        var item = new TransactionInformation();
        foreach (var child in rootChildren.Children())
        {
            item.Type ??= (string)child["Type"];
            item.Action ??= (string) child["Action"];
            item.BuyerBroker ??= (string)child["Buyer/Broker"];
            item.Owner ??= (string)child["Owner"];
            item.CompensationToBuyer ??= (long?)child["Compensation to Buyer"];
        }

        return new Transaction {TransactionInformation = new[] {item}};
    }

    public override bool CanRead => true;

}

and then call it:然后调用它:

var tx =  JsonConvert.DeserializeObject<Transaction>(str, new TransactionConverter());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM