关注DataTable数据(" |"是与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。我可以帮忙吗?
答案 0 :(得分:1)
您需要使用JavascriptSerializer。
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(lstCustom));
在调用API时的JQuery函数中,如果使用Console.log(response.data),则应该看到JSON格式的自定义列表。
您必须创建一个具有与结果集中的列匹配的属性的类。
class Custom
{
public string Col1 {get; set;}
....
public List<int> Col3 {get; set;}
....
}
然后迭代结果集中的列以构造自定义列表对象(lstCustom)。与上面的js.Serialize()一起使用。
答案 1 :(得分:0)
我认为问题在于您的列表未被序列化且contenttype为null,因此浏览器不确定如何对待它。请参阅此答案,希望这对您也有帮助:Return a JSON string explicitly from Asp.net WEBAPI?