如何在ASP.Net中为多层Json格式创建模型? [英] How to create Models in ASP.Net for multi level Json format?
问题描述
问题规范:在asp.net中构建一个Web API,以发布多级json格式的数据.
Problem Specification: Build a web api in asp.net to post multi level json formatted data.
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:
我的模特:
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格式:
Those models Generates this kind of Json Format:
{
"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控制器.
I need those parentheses exactly like the problem specification.What to do? I am working with ASP.Net Web API controller.
推荐答案
您只需复制JSON,然后创建C#Class文件.然后单击编辑"->选择性粘贴"->将JSON粘贴为类".就这样...
You just copy your JSON then create C# Class file. Then click Edit -> Paste Special -> Paste JSON as Classes. Thats it...
为此,您需要Visual Studio.
You need Visual Studio for this.
有关更多信息,请参见下面的链接
如何显示粘贴Json类";在Visual Studio 2012中单击选择性粘贴"?
有效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; }
}
这篇关于如何在ASP.Net中为多层Json格式创建模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!