【问题标题】:What's the syntax to create an object that is an array of objects with JObject?使用 JObject 创建作为对象数组的对象的语法是什么?
【发布时间】:2018-05-21 21:20:37
【问题描述】:

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

这编译正常,但是当我运行时出现运行时错误“对象序列化为数组。需要 JObject 实例。”我认为 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
  }]
}

【问题讨论】:

标签: c# json azure-cosmosdb


【解决方案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"
}
]

【讨论】:

    【解决方案2】:

    你需要知道你想创建什么类型的JToken。例如,您可以从字符串值或 .NET object 创建 JObject。它们就像工厂方法。例如,如果你想从 List&lt;string&gt; 创建它,你必须告诉框架你将从内存中的 object 创建一个 JArray

    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 了解各种令牌。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-20
          • 1970-01-01
          • 2021-12-20
          • 2015-03-23
          • 1970-01-01
          • 1970-01-01
          • 2017-04-06
          • 1970-01-01
          相关资源
          最近更新 更多