我像这样建立我的列表:
public static ListSearchData(string searchString) { var searchResults = new List (); SqlDataReader drResults = FormulaUtility.SearchFormulas(searchString); if ((drResults != null) && (drResults.HasRows)) { while (drResults.Read()) { searchResults.Add(new SearchFormula() { // id use the GetValue function Title = drResults.GetString(1), Description = drResults.GetString(2), Url = drResults.GetString(3) // total use the GetValue Function }); } } return searchResults; }
使用此对象:
public class SearchFormula { public string Title { get; set; } public string Description { get; set; } public string Url { get; set; } }
我开始使用IHttpActionResult,返回OK(结果); 功能.我相信这是让我陷入困惑之路的原因.我已成功发送了一个ArrayList,但这并没有像我想象的那样序列化.
我尝试将其更改为ActionResult并尝试返回Json(结果)Result为实际列表.
我想继续使用IhttpActionResult并使用OK()方法发送序列化数据.我似乎也在内置的json序列化器和NewtonSoft json序列化器之间发生冲突.
我该怎么用 序列化通用列表并将结果传递给IHttpActionResult OK()方法的最简单方法是什么?
我尝试了JavaScriptSerializer,但它返回XML而不是Json ...
public class SearchController : ApiController { public IHttpActionResult Get(string searchTerm) { var jsonSerialiser = new JavaScriptSerializer(); var jsonResult = jsonSerialiser.Serialize(SearchUtility.SearchData(searchTerm)); if (jsonResult != null) { return Ok(jsonResult); } return NotFound(); } }
这是Json.Net示例:
public class SearchController : ApiController { public IHttpActionResult Get(string searchTerm) { var jsonResult = JsonConvert.SerializeObject(SearchUtility.SearchData(searchTerm)); if (jsonResult != null) { return Ok(jsonResult); } return NotFound(); } }
我已经尝试过MemoryStream ......等等等等......没有什么比干净,直截了当的方法更有效,并没有针对这个特定解决方案的主题.
让我从这开始......
如何将通用列表序列化为Json?
如何通过IHttpActionResult发送结果?
*更新*
这是我从Json.Net获取序列化的内容.但格式有问题......甚至Fiddler也无法确定它是Json.我的标题看起来像这样(在Fiddler中):
接受:application/json,text/javascript,/ ; Q = 0.01
"[{\"称号\ ":\" Lacidofil ®\"\ "描述\":\" Lacidofil®设有研究所罗塞尔的瑞士乳杆菌和鼠李糖乳杆菌.这两种菌株都已在人体临床试验中进行了广泛研究,具有... \",\"url \":\"/ products/product-detail.aspx?pid = 103 \"},{\"title\":"MedCaps GI™\","描述\":\"MedCaps GI™的特色成分旨在营养支持胃肠道内层的完整性和最佳功能.富含营养物质,如l-glutam ...... \",\"url \":\"/ products/product-detail.aspx?pid = 114 \"},{\"title \":\"OrganiX™PhytoFood ™\","描述\":"OrganiX PhytoFood是一种方便的粉状配方,提供关键营养素,以支持健康的生活方式.这个全面的公式融合了一个创新的组合...... \",\"url \":\"/ products/product-detail.aspx?pid = 271 \"},{\"title \":\"Probio Defense ™\","描述\":\"Probio Defense™是支持免疫系统的益生菌的最佳组合.\ r \n该产品包含:\ r \n\r \n瑞士乳杆菌Rosell-52(30亿) )\ r \nLactobacillu ... \",\"url \":\"/ products/product-detail.aspx?pid = 102 \"},{\"title \":\"ProbioMax Daily DF™\" ,"描述":"ProbioMax Daily DF™是一种素食,不含乳制品和麸质的四菌株益生菌,每粒胶囊含300亿CFU†.每个素食胶囊用氮气吹扫密封... \",\"url \":\"/ products/product-detail.aspx?pid = 181 \"},{\"title \":\"ProbioMax DF™\","描述\":\"ProbioMax DF™是一种素食,不含乳制品和麸质的四菌株益生菌,每粒胶囊含量为1000亿CFU†.每个素食胶囊都用氮气吹扫的铝封口...... \",\"url \":\"/ products/product-detail.aspx?pid = 184 \"},{\"title \":\"ProbioMax加上DF™\","描述\":"通过个体补充益生菌菌株,非致病性酵母,布拉氏酵母菌,免疫球蛋白,......","网络获得的众多健康益处\":\"/ products/product-detail.aspx?pid = 185 \"},{\"title \":\"Saccharomycin DF™\",\"description \":\"Saccharomycin DF™是一种乳糖 - 免费,耐胃酸,稳定,欧洲正在申请专利的配方,含有经过DNA验证的布拉氏酵母菌(Saccharomyces boulardii).这种益生菌酵母支持... \",\"url \":\"/ products/product-detail.aspx?pid = 197 \"}]"
hutchonoid.. 10
我采用这种方法似乎更简单,并且不涉及为您拥有的数据更改json序列化程序.
如果将对象作为列表返回,则默认媒体类型格式化程序将根据从客户端指定的内容类型处理序列化(假设它是json或xml).
出于演示目的,添加以下返回硬编码对象的方法.
// GET api/search public ListGet(string searchTerm) { var searchItems = SearchData(searchTerm); return searchItems; } public static List SearchData(string searchString) { var searchResults = new List (); searchResults.Add(new SearchFormula { Description = "desc1", Title = "title1", Url = "http://url.com" }); searchResults.Add(new SearchFormula { Description = "desc2", Title = "title2", Url = "http://url.com" }); return searchResults; }
然后在fiddler中指定客户端application/json
如下图所示接受,并将内容返回为json
.
有关序列化的更多信息,请参见此处:
http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization