Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn Json from Generic List in Web API
    primarykey
    data
    text
    <p>I build my list like this:</p> <pre><code>public static List&lt;SearchFormula&gt; SearchData(string searchString) { var searchResults = new List&lt;SearchFormula&gt;(); SqlDataReader drResults = FormulaUtility.SearchFormulas(searchString); if ((drResults != null) &amp;&amp; (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; } </code></pre> <p>Using this Object:</p> <pre><code>public class SearchFormula { public string Title { get; set; } public string Description { get; set; } public string Url { get; set; } } </code></pre> <p>I began using the IHttpActionResult, returning the OK(results); function. I believe this is what started me down the confusing road. I had successfully sent an ArrayList but this did not serialize the way I thought it would.</p> <p>I tried changing it to ActionResult and attempted to return Json(result) Result being the actual list.</p> <p>I would like to continue to use the IhttpActionResult and send the serialized data with the OK() method. I also seem to be having a conflict between the built-in json serializer and NewtonSoft json serializer.</p> <p>What should I use. What is the simplest way of just serializing a generic list and passing the result into the IHttpActionResult OK() method?</p> <p>I tried the JavaScriptSerializer but it returns XML not Json...</p> <pre><code>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(); } } </code></pre> <p>Here is the Json.Net Example:</p> <pre><code>public class SearchController : ApiController { public IHttpActionResult Get(string searchTerm) { var jsonResult = JsonConvert.SerializeObject(SearchUtility.SearchData(searchTerm)); if (jsonResult != null) { return Ok(jsonResult); } return NotFound(); } } </code></pre> <p>I have tried the MemoryStream... blah blah blah... nothing seems like a clean, straightforward approach and there is no subject matter for this specific solution.</p> <p>Let me start with this... </p> <p>How can I serialize a Generic list to Json?</p> <p>How can I send that result through the IHttpActionResult?</p> <blockquote> <p>*<strong><em>Update</em>*</strong></p> </blockquote> <p>This is what I am getting for the serialization from Json.Net. BUT something is wrong with the format... Even Fiddler can not determine that it is Json. My Header looks like this (in Fiddler):</p> <p><strong>Accept: application/json, text/javascript, <em>/</em>; q=0.01</strong></p> <blockquote> <p>"[{\"title\":\"Lacidofil<sup>®</sup>\",\"description\":\"Lacidofil&reg; features Institut Rosell&rsquo;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&trade; 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&nbsp;nutrients to support a healthy lifestyle. This comprehensive formula incorporates&nbsp;an innovative blend of organi...\",\"url\":\"/products/product-detail.aspx?pid=271\"},{\"title\":\"Probio Defense™\",\"description\":\"Probio Defense&trade; 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&trade; is a vegetarian, dairy- and gluten-free, four-strain probiotic totaling 30 billion CFU&dagger; per capsule. Each vegetarian capsule is sealed in nitrogen-purged alu...\",\"url\":\"/products/product-detail.aspx?pid=181\"},{\"title\":\"ProbioMax DF™\",\"description\":\"ProbioMax DF&trade; is a vegetarian, dairy- and gluten-free, four-strain probiotic totaling 100 billion CFU&dagger; 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&nbsp;supplementation of probiotic strains of bacteria, the non-pathogenic&nbsp;yeast, Saccharomyces boulardii, immunoglobulins,...\",\"url\":\"/products/product-detail.aspx?pid=185\"},{\"title\":\"Saccharomycin DF™\",\"description\":\"Saccharomycin DF&trade; is a lactose-free, stomach acid&ndash;resistant, stable, European patent-pending formula containing DNA-verified Saccharomyces boulardii. This probiotic yeast support...\",\"url\":\"/products/product-detail.aspx?pid=197\"}]"</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload