C# 如何在Web API中正确创建JSON文件

C# 如何在Web API中正确创建JSON文件,c#,json,asp.net-web-api,C#,Json,Asp.net Web Api,以下数据表数据|是匹配相应Col3到Col4/Col5的分隔符: Col1 Col2 Col3 Col4 Col5 John Doe 12 156-345 792-098 Mike Keller 12|15 145-394|909-

以下数据表数据|是匹配相应Col3到Col4/Col5的分隔符:

Col1        Col2        Col3            Col4                                Col5
John        Doe         12              156-345                             792-098
Mike        Keller      12|15           145-394|909-203                     156-323|121-444,134-232
Hanes       Wara        34|12|18        180-655,202-175|123-654|118-000     121-343|654-222|109-220
我目前有一个自定义列表,在API调用中仅显示如下数据:

[HttpGet]
public System.Web.Mvc.JsonResult RTData()
{
    //...call SQL to retrieve the data and populate the {custom List}
    //return...
    return new System.Web.Mvc.JsonResult { Data = {custom List} };
}
这是我从JQuery调用API时在浏览器控制台中得到的结果:

Object
    ContentEncoding:null
    ContentType:null
    Data:Array(12)
        [0 … 11]
        length:12
        __proto__:Array(0)
    JsonRequestBehavior:1
    MaxJsonLength:null
    RecursionLimit:null
    __proto__:Object
数据中的每个结果如下所示:

Data:Array(12)
    [0 … 11]
    0:
        Col1: "John"
        Col2: "Doe"
        Col3: "12"
        Col4: "156-345"
        Col5: "792-098"
    1:
        Col1: "Mike"
        Col2: "Keller"
        Col3: "12|15"
        Col4: "145-394|909-203  "
        Col5: "156-323|121-444,134-232"
所需的JSON输出:

{
    "Col1": "John",
    "Col2": "Doe",
    "Col3Combined": [
        {
            "Col3": "12",
            "Col4Combined": [
                {
                    "Col4": "156-345"
                }
            ],
            "Col5Combined": [
                {
                    "Col5": "792-098"
                }
            ]
        }
    ]
},
{
    "Col1": "Mike",
    "Col2": "Keller",
    "Col3Combined": [
        {
            "Col3": "12",
            "Col4Combined": [
                {
                    "Col4": "145-394"
                }
            ],
            "Col5Combined": [
                {
                    "Col5": "156-323"
                }
            ]
        },
        {
            "Col3": "15",
            "Col4Combined": [
                {
                    "Col4": "909-203"
                }
            ],
            "Col5Combined": [
                {
                    "Col5": "121-444",
                    "Col5": "134-232"
                }
            ]
        }
    ]
}...//more data
如何在API调用中实现JSON格式

我上的那门课应该够了吧

public class RootObject
{
    public string col1 { get; set; }
    public string col2 { get; set; }
    public List<col3data> col3 { get; set; }
}

public class col3data
{
    public string col3d { get; set; }
    public List<col4data> col4d { get; set; }
    public List<col5data> col5d { get; set; }
}

public class col4data
{
    public string col4 { get; set; } //since col4 can also have comma separated values within each entry, as seen for Hanes, should this be a list too?
}

public class col5data
{
    public string col5 { get; set; } //since col5 can also have comma separated values within each entry, as seen for Mike, should this be a list too?
}
类似这样的事情也有:

public class RootObject
{
    public string col1 { get; set; }
    public string col2 { get; set; }
    public List<col3data> col3 { get; set; }
}

public class col3data
{
    public string col3d { get; set; }
    public List<col4data> col4d { get; set; }
    public List<col5data> col5d { get; set; }
}

public class col4data
{
    public List<col4subdata> col4sub { get; set; }
}

public class col4subdata
{
    public string col4_1 { get; set; }
    public string col4_2 { get; set; }
}

public class col5data
{
    public List<col5subdata> col5sub { get; set; }
}

public class col5subdata
{
    public string col5_1 { get; set; }
    public string col5_2 { get; set; }
}

我想现在我必须遍历每一行,添加到RootObject类中,并从中创建一个JSON。我能得到一些帮助吗?

您需要使用JavascriptSerializer

JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(lstCustom));
在调用API时的JQuery函数中,如果使用Console.logresponse.data,应该可以看到JSON格式的自定义列表

您必须创建一个类,该类的属性与resultset中的列相匹配

class Custom
{
    public string Col1 {get; set;}
    ....
    public List<int> Col3 {get; set;}
    ....
}

然后在结果集中的列上迭代以构造自定义列表对象lstCustom。将其与上面的js.Serialize一起使用。

我认为问题在于您的列表没有序列化,contenttype为null,因此浏览器不确定如何处理它。看看这个问题的答案,希望这也能帮助你:

谢谢你的回复。注意数据是如何进来的,我必须把它们分开。另外,当我使用JSSerializer时,我会绕过我的JSON。默认情况下,Web API已经使用JSON.NET将数据序列化为JSON。但是列数据位于不同的位置,那么逻辑是如何工作的呢?创建一个自定义对象列表,如list lstCustom=new list;然后,从sql查询中获得结果集后,对结果集进行迭代,为每一行创建自定义对象并将其添加到lstCustom列表中。我添加了应该可以工作的类,您是否可以让我知道您的反馈。请在浏览器控制台中显示如何获得该输出。console.logJSON.stringifydata;或者类似的,取决于变量名,应该显示为JSON。您还可以在浏览器的“网络”选项卡中查看服务器的原始响应。如果只将JS对象直接记录到控制台而不进行字符串化,那么该输出看起来就像是您可能得到的。请记住,您的代码可能会自动将JSON字符串转换为JS对象。您使用的是什么版本的Web API?网络API 2或核心?网络API 2。。。我只是想澄清一下,我只是想请求帮助从主键创建子键并进行迭代。一切都对我有效,但我想创建嵌套键,正如您从所需结果中看到的那样。好的,很酷。正如其他人现在所说的,嵌套键需要作为集合类型列表等位于对象上。。一旦序列化该项目并返回序列化的json对象,它将正确地嵌套它们。