简体   繁体   English

如何在ASP.Net中为多层Json格式创建模型?

[英]How to create Models in ASP.Net for multi level Json format?

Problem Specification: Build a web api in asp.net to post multi level json formatted data. 问题规范:在asp.net中构建一个Web api以发布多级json格式的数据。

Json Format: Json格式:

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

I tried to solve this problem. 我试图解决这个问题。 My models are given bellow: 我的模型如下所示:

My Models: 我的模特:

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; }

    }

Those models Generates this kind of Json Format: 这些模型生成这种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"
  }
}

I need those parentheses exactly like the problem specification.What to do? 我需要那些括号完全像问题说明一样。该怎么办? I am working with ASP.Net Web API controller. 我正在使用ASP.Net Web API控制器。

You're inheriting Information from InfoList so naturally it will get the fields Name , URL etc. Remove the inheritance and change the InfoList and Info members into arrays or lists and you get what you want. 您是从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; }
}

You just copy your JSON then create C# Class file. 您只需复制JSON,然后创建C#类文件。 Then click Edit -> Paste Special -> Paste JSON as Classes. 然后单击编辑->选择性粘贴-> JSON粘贴为类。 Thats it... 而已...

You need Visual Studio for this. 您需要为此使用Visual Studio。

For more information please see below link 有关更多信息,请参见下面的链接

How to show the "paste Json class" in visual studio 2012 when clicking on Paste Special? 单击选择性粘贴时,如何在Visual Studio 2012中显示“粘贴Json类”?

Valid JSON 有效的JSON

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

So your class must be like this. 因此,您的课程必须是这样的。

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; }
}

You can use http://json2csharp.com/ online tool to create c# class from json data string. 您可以使用http://json2csharp.com/在线工具从json数据字符串创建c#类。 Just copy your json and get classes. 只需复制您的json并获取类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM