C# 什么';使用JObject创建对象数组的语法是什么?

C# 什么';使用JObject创建对象数组的语法是什么?,c#,json,azure-cosmosdb,C#,Json,Azure Cosmosdb,我正在为Cosmos DB(基于文档或JSON的DB)创建一个数据库种子。一些C#模型的属性配置为JSON,因此我一直在使用这种类型的代码来设置该属性: Config = JObject.FromObject(new { }) 这与在对象内部实际设置属性一样: Config = JObject.FromObject(new { contextOptionSource = "$.domains.governmentEntityType_active" }), 但是,我不

我正在为Cosmos DB(基于文档或JSON的DB)创建一个数据库种子。一些C#模型的属性配置为JSON,因此我一直在使用这种类型的代码来设置该属性:

Config = JObject.FromObject(new { })
这与在对象内部实际设置属性一样:

Config = JObject.FromObject(new
{
  contextOptionSource = "$.domains.governmentEntityType_active"
}),
但是,我不知道如何将Config设置为对象数组。我尝试实际使用C#模型,认为JObject会像这样为我转换它们:

Config = JObject.FromObject(
  new List<Question>
  {
    new Question
    {
      Key = "contact",
      Label = "Contact Person",
      HelpText = "",
      Config = JObject.FromObject(new {}),
      Type = "text",
      ContextTarget = "$.data.contact"
    },
    new Question
    {
      Key = "company",
      Label = "Company Name",
      HelpText = "",
      Config = JObject.FromObject(new {}),
      Type = "text",
      ContextTarget = "$.data.company"
    }
  }),

您需要知道要创建哪种类型的
JToken
。例如,您可以从字符串值或.NET
对象创建
JObject
。它们就像工厂方法。例如,如果要从
列表创建它,则必须告诉框架您将从内存中的
对象创建
JArray

var fromArray = JArray.FromObject(new List<string>
                {
                    "One",
                    "Two"
                });
var fromObj = JObject.FromObject(new Customer() { Name = "Jon" });
或者,您可以从JSON创建令牌:

var fromJson = JArray.Parse("[\"One\", \"Two\"]");
最后,您可以创建
JToken
,它是上述所有类(
JArray
JObject
)的父类,但您只能访问
JToken
属性和方法:

var Config = JToken.FromObject(
    new List<string>
    {
        "One",
        "Two"
    });
var Config=JToken.FromObject(
新名单
{
“一个”,
“两个”
});

有关各种令牌,请参阅。

尝试
JToken
JArray

var Config = JToken.FromObject(
    new List<Question>
    {
        new Question
        {
            Key = "contact",
            Label = "Contact Person",
            HelpText = "",
            Config = JObject.FromObject(new {}),
            Type = "text",
            ContextTarget = "$.data.contact"
        },
        new Question
        {
            Key = "company",
            Label = "Company Name",
            HelpText = "",
            Config = JObject.FromObject(new {}),
            Type = "text",
            ContextTarget = "$.data.company"
        }
    }
    );

var result = Config.ToString();

使用
JArray
,您可以选择最适合您需要的构造函数

,因此我需要这样做的C代码是:

Config = JObject.FromObject(new
                                            {
                                                questions = JArray.FromObject(
                                                new List<Question>
                                                {
                                                    new Question
                                                    {
                                                        Key = "contact",
                                                        Label = "Contact Person",
                                                        HelpText = "",
                                                        Config = emptyJObject,
                                                        Type = "text",
                                                        ContextTarget = "$.data.contact"
                                                    },
                                                    new Question
                                                    {
                                                        Key = "company",
                                                        Label = "Company Name",
                                                        HelpText = "",
                                                        Config = emptyJObject,
                                                        Type = "text",
                                                        ContextTarget = "$.data.company"
                                                    }
                                                })
                                            })
Config=JObject.FromObject(新建)
{
questions=JArray.FromObject(
新名单
{
新问题
{
Key=“contact”,
Label=“联系人”,
HelpText=“”,
Config=emptyJObject,
Type=“text”,
ContextTarget=“$.data.contact”
},
新问题
{
Key=“公司”,
Label=“公司名称”,
HelpText=“”,
Config=emptyJObject,
Type=“text”,
ContextTarget=“$.data.company”
}
})
})

Config
必须是
JToken
(或
JContainer
)类型,而不是
JObject
。您可以使用来生成它。看,还有。如果我把问题说得更清楚,这就是答案。见我下面的解释。请编辑你的问题,包括对你自己答案预期结果的澄清。也。看见
Config = JObject.FromObject(new
                                            {
                                                questions = JArray.FromObject(
                                                new List<Question>
                                                {
                                                    new Question
                                                    {
                                                        Key = "contact",
                                                        Label = "Contact Person",
                                                        HelpText = "",
                                                        Config = emptyJObject,
                                                        Type = "text",
                                                        ContextTarget = "$.data.contact"
                                                    },
                                                    new Question
                                                    {
                                                        Key = "company",
                                                        Label = "Company Name",
                                                        HelpText = "",
                                                        Config = emptyJObject,
                                                        Type = "text",
                                                        ContextTarget = "$.data.company"
                                                    }
                                                })
                                            })