扁平化反序列化结构

时间:2013-11-10 13:11:29

标签: .net json.net deserialization

我有一些JSON,我想使用Json.Net反序列化,但我不想模仿JSON对象结构。例如,我有这个订单对象:

{
    "order":
    {
        "order_id": "111-22222222-33",
        "account_identifier": "my account id",
        "customer": "my customer",
        "sku": "WALM-E-500-STD",
        "amount": 500,
        "reward_message": "my message is awesome",
        "reward_subject": "my subject is awesome",
        "reward_from": "me",
        "delivered_at": "2013-11-10T12:49:00+00:00",
        "recipient": {
          "name": "me",
          "email": "my@email.com"
        },
        "reward": {
          "token": "1234567890.1234567890",
          "number": "A1234567890",
          "redemption_url": "http://some.domain.com/r/1234567890"
        }
    }
}

我不想要recipientreward属性,而是希望将这些属性展平,以便nameemailtoken,{{1 }}和number成为订单的属性。这可能吗?

1 个答案:

答案 0 :(得分:1)

在Stackoverflow上尝试另一个答案:How to Convert JSON object to Custom C# object?

它显示了一种在构造函数中使用JSON.NET实例化对象属性的方法。通过这样做,您可以将结构中的数据单独映射到对象中的相应属性。