【问题标题】:How to create Models in ASP.Net for multi level Json format?如何在 ASP.Net 中为多级 Json 格式创建模型?
【发布时间】:2017-02-22 04:49:04
【问题描述】:

问题说明:在asp.net中构建一个web api来发布多级json格式的数据。

Json 格式:

{
  "InfoList:"[
    {
    "Name": " Sample String 1",
    "URL": " Sample String2",
    "Info":[
        {
            "ZIP": " Sample String3 "
            "status": "Sample String4"
        }
    ]
     }
   ]
}

我试图解决这个问题。我的模型如下:

我的模特:

public class Information : InfoList
    {
        public InfoList InfoList { get; set; }
    }

public class InfoList 
        {

            public string Name { get; set; }

            public string URL { get; set; }

            public Info info { get; set; }
        }

public class Info
    {
        public string ZIP{ get; set; }

        public string status { get; set; }

    }

那些模型生成这种Json格式:

{
  "InfoList": {
    "Name": "sample string 1",
    "URL": "sample string 2",
    "Info": {
      "ZIP": "sample string 1",
      "status": "sample string 2"
    }
  },
  "Name": "sample string 1",
  "URL": "sample string 2",
  "Info": {
    "ZIP": "sample string 1",
    "status": "sample string 2"
  }
}

我需要与问题规范完全相同的括号。该怎么办?我正在使用 ASP.Net Web API 控制器。

【问题讨论】:

  • 您需要制作属性集合 - public IEnumerable<InfoList> InfoList { get; set; }public IEnumerable<Info> info { get; set; }

标签: json asp.net-mvc asp.net-web-api json2csharp


【解决方案1】:

您从InfoList 继承Information,因此它自然会得到NameURL 等字段。删除继承并将InfoListInfo 成员更改为数组或列表,然后您得到你想要的。

public class Information
{
    public InfoList[] InfoList { get; set; }
}

public class InfoList 
{
    public string Name { get; set; }
    public string URL { get; set; }
    public Info[] info { get; set; }
}

public class Info
{
    public string ZIP{ get; set; }
    public string status { get; set; }
}

【讨论】:

  • 你好萨米。此代码返回 s me bellow json 格式 - >
  • 这段代码没有给我确切的 Json 格式。我的发布方法: public string Post([FromBody]Information value) { string hello;返回“价值”;我的 post 方法有什么问题吗??
  • @Pongta 那段代码不会返回任何 json 格式的东西,所以很难说...
  • 抱歉我的错误评论。基本上我试图说点别的。顺便说一句,我的问题解决了。您的课程正常工作。谢谢。
【解决方案2】:

您只需复制您的 JSON,然后创建 C# 类文件。然后单击编辑 -> 选择性粘贴 -> 将 JSON 粘贴为类。就是这样……

为此,您需要 Visual Studio。

更多信息请查看以下链接

How to show the "paste Json class" in visual studio 2012 when clicking on Paste Special?

有效的 JSON

{
    "InfoList":  {
        "Name": " Sample String 1",
        "URL": " Sample String2",
        "Info": [
            {
                "ZIP": " Sample String3 ",
                "status": "Sample String4"
            }
        ]
    }
}

所以你的班级一定是这样的。

public class Rootobject
{
    public Infolist InfoList { get; set; }
}

public class Infolist
{
    public string Name { get; set; }
    public string URL { get; set; }
    public Info[] Info { get; set; }
}

public class Info
{
    public string ZIP { get; set; }
    public string status { get; set; }
}

【讨论】:

  • 您可能要提到这需要 Visual Studio。这不是 C# 固有的。
【解决方案3】:

您可以使用http://json2csharp.com/在线工具从json数据字符串创建c#类。只需复制您的 json 并获取课程。

【讨论】:

    猜你喜欢
    • 2019-06-26
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    相关资源
    最近更新 更多