如何将c#对象序列化为json,使其看起来像"["starts-with", "

本文关键字:quot starts-with 看起来 序列化 json 对象 | 更新日期: 2023-09-27 18:06:48

我试图在c#中创建一个视图模型,可以序列化为amazon S3所需的json文档。这里的文档。其中一个属性是这样的

 ["starts-with", "$key", "user/john/"] 

c#对象应该是什么样子,当它被序列化时,它会是这样的?文档的其余部分看起来像这样。

    { "expiration": "2007-12-01T12:00:00.000Z",
  "conditions": [
    {"acl": "public-read" },
    {"bucket": "johnsmith" },
    ["starts-with", "$key", "user/john/"],
  ]
}

如何将c#对象序列化为json,使其看起来像"["starts-with", "

使用字符串数组

string[] data = new string[] { "starts-with", "$key", "user/john/" };

第二部分的对象结构类似于

public class S3Expiration {
    DateTime expiration { get; set; }
    object[] conditions { get; set; }
}

要填充它,你可以这样写

var expiration = new S3Expiration {
    expiration = DateTime.Now,
    conditions = new object[] {
        new { acl = "public-read" },
        new { bucket = "johnsmith" },
        new string[] { "starts-with", "$key", "user/john/" }
    }
};