Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting invalid string [ ] around my JSON outputted from using the JavascriptSerializer class
    text
    copied!<p>Currently I'm using the helper methods outlined here to return some JSON from my .ashx: <a href="http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx</a></p> <p>Problem is, I get [ and ] wrapped around my JSON which is malformed...jQuery cannot pick it up in the callback:</p> <pre><code>[{"ImageTag":"&amp;lt;img src=&amp;quot;http://www.xxx.com/image/473.jpg&amp;quot; alt=&amp;quot;&amp;quot;&amp;gt;"},{"ImageTag":"&amp;lt;img src=&amp;quot;http://www.xxx.com/image/485.jpg&amp;quot; alt=&amp;quot;&amp;quot;&amp;gt;"}] </code></pre> <p>So I don't know why I get brackets around this. Here is my implementation:</p> <pre><code>private void GetImagesJSON(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Charset = Encoding.UTF8.ToString(); int i = 1; List&lt;Product&gt; products = GetTestProducts(); List&lt;CtImageList&gt; imageList = new List&lt;CtImageList&gt;(); foreach(Product p in products) { string imageTag = HttpUtility.HtmlEncode(string.Format(@"&lt;img src=""{0}"" alt=""""&gt;", ImageUrl(p.Image, false))); imageList.Add(new CtImageList{ImageTag = imageTag}); i++; } string jsonString = imageList.ToJSON(); context.Response.Write(jsonString); } </code></pre> <p>Here is the callback function in jQuery which can't parse that because of the starting [ and ]:</p> <pre><code>function itemLoadCallback(carousel, state) { // Only load items if they don't already exist if (carousel.has(carousel.first, carousel.last)) { return; } $.getJSON("http://localhost:59396/xxx/CHandler.ashx?action=productsjson", function(data) { $.each(data.items, function(i, item) { alert('got here'); carousel.add(i, mycarousel_decodeEntities(item.ImageTag)); if (i == 3) return false; }); }); }; </code></pre>
 

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