Returned JSONproblem from JavascriptSerializer adds [ and ]

Last post 06-29-2009 12:33 PM by gunteman. 1 replies.

Sort Posts:

  • Returned JSONproblem from JavascriptSerializer adds [ and ]

    06-29-2009, 12:11 PM
    • Contributor
      2,724 point Contributor
    • dba123
    • Member since 12-13-2003, 12:04 AM
    • Posts 1,336

    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});
                i++;
            }
    
            string jsonString = imageList.ToJSON();
            context.Response.Write(jsonString);
        }


    When is Microsoft going to get rid of VB.NET!
  • Re: Returned JSONproblem from JavascriptSerializer adds [ and ]

    06-29-2009, 12:33 PM
    Answer
    • All-Star
      21,646 point All-Star
    • gunteman
    • Member since 07-11-2007, 8:57 AM
    • Norrköping, Sweden
    • Posts 3,176

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

    -- "Mark As Answer" if my reply helped you --
Page 1 of 1 (2 items)