Returned JSONproblem from JavascriptSerializer adds [ and ] RRS feed

  • Question

  • User1982844092 posted

    Currently I'm using the helper methods outlined here to return some JSON from my .ashx: http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx

    Problem is, I get [ and ] wrapped around my JSON which is malformed...jQuery cannot pick it up in the callback:

    [{"ImageTag":"<img src="http://www.xxx.com/image/473.jpg" alt="">"},{"ImageTag":"<img src="http://www.xxx.com/image/485.jpg" alt="">"}]

    So I don't know why I get brackets around this.  Here is my implementation:

        private void GetImagesJSON(HttpContext context)
            context.Response.ContentType = "text/plain";
            context.Response.Charset = Encoding.UTF8.ToString();
            int i = 1;
            List<Product> products = GetTestProducts();
            List<CtImageList> imageList = new List<CtImageList>();
            foreach(Product p in products)
                string imageTag = HttpUtility.HtmlEncode(string.Format(@"<img src=""{0}"" alt="""">", ImageUrl(p.Image, false)));
                imageList.Add(new CtImageList{ImageTag = imageTag});
            string jsonString = imageList.ToJSON();

    Monday, June 29, 2009 12:11 PM


  • User437720957 posted

    [ and ] is proper JSON array notation. The problem is in your consuming code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 29, 2009 12:33 PM