【问题标题】:How to create this schema like model in c#?如何在 C# 中创建类似模型的模式?
【发布时间】:2021-10-06 02:33:13
【问题描述】:

我想使用 c# 模型创建模式输出。 示例架构


Food:[["burger", "french fries", "nuggets"], "status": "delivered"]

型号:


Public class Food
{
public List<item> items {get; set;}
}

public class item
{
public List<string> food_item {get; set;}

public string status {get; set;}
}

【问题讨论】:

  • 这看起来不像是有效的 json。数组不能有属性。一个对象将是有效的; Food: { "food_item" : [...], "status": "delivered"}
  • 您的 JSON 看起来不完整 - 您能否验证您的 JSON?

标签: c# .net entity-framework


【解决方案1】:

正确的 json 应该如下所示

"Food":[{"food_item":["burger", "french fries", "nuggets"], "status": "delivered"}]

以及模型(使用https://json2csharp.com/ 来验证您的json 并将其转换为模型)

public class Food
{
    public List<string> food_item { get; set; }
    public string status { get; set; }
}

public class Root
{
    public List<Food> Food { get; set; }
}

使用下面的代码反序列化

Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 

【讨论】:

  • 我不想让它成为正确的 json。输出格式应该是这样的。总之谢谢
猜你喜欢
  • 2017-12-01
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多