Skip to main content
 首页 » 编程设计

c#之如何将 C# 对象序列化为 json,使其看起来像 "["开头为", "$key", "user/john/"]"

2024年02月13日12jyk

我正在尝试在 C# 中创建一个 View 模型,它可以序列化为 amazon S3 所需的 json 文档。文档 here .其中一个属性看起来像这样,

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

C# 对象看起来像什么,所以当它被序列化时会像这样出现? 文档的其余部分如下所示。

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

请您参考如下方法:

就用一个字符串数组

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