我是这样构建我的列表的:
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(results)函数。我相信这是让我进入困惑之路的原因。我已经成功地发送了一个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中):
Accept: application/json, text/javascript, /; q=0.01
"[{\"title\":\"Lacidofil®\",\"description\":\"Lacidofil® features Institut Rosell’s Lactobacillus helveticus and Lactobacillus rhamnosus. Both of these strains have been extensively studied in human clinical trials, possess an...\",\"url\":\"/products/product-detail.aspx?pid=103\"},{\"title\":\"MedCaps GI™\",\"description\":\"MedCaps GI™ features ingredients that are designed to nutritionally support the integrity and optimal function of the gastrointestinal lining. Fortified with nutrients such as l-glutam...\",\"url\":\"/products/product-detail.aspx?pid=114\"},{\"title\":\"OrganiX™ PhytoFood™\",\"description\":\"OrganiX PhytoFood is a convenient powdered formulation providing key nutrients to support a healthy lifestyle. This comprehensive formula incorporates an innovative blend of organi...\",\"url\":\"/products/product-detail.aspx?pid=271\"},{\"title\":\"Probio Defense™\",\"description\":\"Probio Defense™ is an optimal combination of probiotic bacteria that supports the immune system.\r\nThis product contains:\r\n\r\nLactobacillus helveticus Rosell-52 (3 billion)\r\nLactobacillu...\",\"url\":\"/products/product-detail.aspx?pid=102\"},{\"title\":\"ProbioMax Daily DF™\",\"description\":\"ProbioMax Daily DF™ is a vegetarian, dairy- and gluten-free, four-strain probiotic totaling 30 billion CFU† per capsule. Each vegetarian capsule is sealed in nitrogen-purged alu...\",\"url\":\"/products/product-detail.aspx?pid=181\"},{\"title\":\"ProbioMax DF™\",\"description\":\"ProbioMax DF™ is a vegetarian, dairy- and gluten-free, four-strain probiotic totaling 100 billion CFU† per capsule. Each vegetarian capsule is sealed in nitrogen-purged aluminum...\",\"url\":\"/products/product-detail.aspx?pid=184\"},{\"title\":\"ProbioMax Plus DF™\",\"description\":\"The multitude of healthful benefits attained by the individual supplementation of probiotic strains of bacteria, the non-pathogenic yeast, Saccharomyces boulardii, immunoglobulins,...\",\"url\":\"/products/product-detail.aspx?pid=185\"},{\"title\":\"Saccharomycin DF™\",\"description\":\"Saccharomycin DF™ is a lactose-free, stomach acid–resistant, stable, European patent-pending formula containing DNA-verified Saccharomyces boulardii. This probiotic yeast support...\",\"url\":\"/products/product-detail.aspx?pid=197\"}]"