使用JObject创建一个对象数组的对象的语法是什么?

时间:2018-05-21 21:20:38

标签: c# json azure-cosmosdb

我正在为Cosmos DB(基于文档或JSON的数据库)创建数据库播种器。一些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"
                                                    }
                                                }),

这编译好了,但是当我运行时,我得到一个运行时错误&#34; Object序列化为Array。期望JObject实例。&#39;&#34;我错误地认为JObject应该将C#模型转换为JSON?如果它们必须是通用对象,但我无法正确使用FromObject方法接受此Config属性中的多个对象。

编辑:这是我试图制作的JSON:

   "config": {
                            "questions": [{
                                    "key": "contact",
                                    "label": "Contact Person",
                                    "helpText": "Contact Person",
                                    "config": {},
                                    "type": QuestionKind.Textbox,
                                    "contextTarget": "$.data.contact",
                                    "enabledRule": null,
                                    "validationRules": [],
                                    "visibleRule": null
                                },
                                {
                                    "key": "company",
                                    "label": "Company Name",
                                    "helpText": "Company Name",
                                    "config": {},
                                    "type": QuestionKind.Textbox,
                                    "contextTarget": "$.data.company",
                                    "enabledRule": null,
                                    "validationRules": [],
                                    "visibleRule": null
                                }
                            ]
                        }

4 个答案:

答案 0 :(得分:1)

尝试JTokenJArray

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();

结果是:

[
{
    "Label": "Contact Person",
    "HelpText": "",
    "Type": "text",
    "ContextTarget": "$.data.contact",
    "Config": {},
    "Key": "contact"
},
{
    "Label": "Company Name",
    "HelpText": "",
    "Type": "text",
    "ContextTarget": "$.data.company",
    "Config": {},
    "Key": "company"
}
]

答案 1 :(得分:0)

您需要知道要创建的JToken类型。例如,您可以从字符串值或.NET JObject创建object。它们就像工厂方法。例如,如果您想要从List<string>创建它,则必须告诉框架您将从内存JArray创建object

var fromArray = JArray.FromObject(new List<string>
                {
                    "One",
                    "Two"
                });

或者您将创建JObject

var fromObj = JObject.FromObject(new Customer() { Name = "Jon" });

或者您可以从JSON创建令牌:

var fromJson = JArray.Parse("[\"One\", \"Two\"]");

最后,您只需创建JToken,它是上述所有类(JArrayJObject)的父级,但您只能访问JToken个属性和方法:

var Config = JToken.FromObject(
    new List<string>
    {
        "One",
        "Two"
    });

有关各种令牌,请参阅this

答案 2 :(得分:0)

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

答案 3 :(得分:0)

所以我需要这样做的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"
                                                    }
                                                })
                                            })