简体   繁体   中英

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. 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? 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/" }
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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