我建立这样的清单:
public static List<SearchFormula> SearchData(string searchString)
{
var searchResults = new List<SearchFormula>();
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)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®具有Rosell的瑞士乳杆菌和鼠李糖乳杆菌。这两种菌株均已在人类临床试验中进行了广泛研究,并具有... \“,\” url \“:\” / products / product-detail.aspx?pid = 103 \“},{\” title \ “:\” MedCaps GI™\“,\”说明\“:\” MedCaps GI™具有旨在营养上支持胃肠道内膜完整性和最佳功能的成分。富含l-谷氨酸等营养素... \“,\” 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 \ n乳酸杆菌... \“,\” URL \“:\” / products / product-detail.aspx?pid = 102 \“},{\”标题\“:\” ProbioMax Daily DF™\“ ProbioMax Daily DF™是一种不含素食,无乳制品和无麸质的四株益生菌,每个胶囊总计300亿CFU†。每个素食胶囊都密封在氮气吹扫过的铝中... \“,\” url \“:\” / products / product-detail.aspx?pid = 181 \“},欧洲专利申请中的配方,包含经DNA验证的酿酒酵母。此益生菌酵母支持... \“,\” url \“:\” / products / product-detail.aspx?pid = 197 \“}]”
我采用这种方法似乎更简单,并且不涉及为您拥有的数据更改json序列化程序。
如果将对象作为列表返回,则默认媒体类型格式化程序将根据客户端指定的内容类型(假设它是json或xml)来处理序列化。
出于演示目的,添加以下返回硬编码对象的方法。
// GET api/search
public List<SearchFormula> Get(string searchTerm)
{
var searchItems = SearchData(searchTerm);
return searchItems;
}
public static List<SearchFormula> SearchData(string searchString)
{
var searchResults = new List<SearchFormula>();
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;
}
然后在提琴手中指定客户端接受application/json
,如下图所示,内容返回为json
。
有关序列化的更多信息,请参见此处:
http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句