简体   繁体   English

如何将C#对象序列化为json,使其看起来像“[”starts-with“,”$ key“,”user / john /“]”?

[英]How to serialize a C# object to json so it looks like “[”starts-with“, ”$key“, ”user/john/“]”?

I'm trying to create a viewmodel in C# that can be serialized into a json document required by amazon S3. 我正在尝试在C#中创建一个可以序列化为amazon S3所需的json文档的viewmodel。 Documentation here . 文档在这里 One of the properties looks like this, 其中一个属性看起来像这样,

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

What would the C# object look like so when it's serialized it would come out like this? C#对象在序列化时会是什么样子,它会像这样出现? Rest of document looks like this. 其余文档看起来像这样。

    { "expiration": "2007-12-01T12:00:00.000Z",

  "conditions": [

    {"acl": "public-read" },

    {"bucket": "johnsmith" },

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

  ]

}

Just use a string array 只需使用字符串数组

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

The object structure for the second part would be something like 第二部分的对象结构就像是

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

and to populate it you would write something like 并填充它你会写类似的东西

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

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

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