locked
Format returning JSON issue from .ashx RRS feed

  • Question

  • User1982844092 posted

    I'm sending back a bunch of image tags via JSON in my .ashx response.

    I am not sure how to format this so that the string comes back with real tags.  I tried to HtmlEncode and that sort of fixed it but then I ended up with this stupid \u003c crap:

    ["\u003cimg src=\"http://www.sss.com/image/65.jpg\" alt=\"\"\u003e\u003c/li\u003e","\u003cimg src=\"http://www.xxx.com/image/61.jpg\" alt=\"\"\u003e\u003c/li\u003e"]

    What the heck is \u003c ?

    here's my code that created the JSON for response to my .ashx:

        private void GetProductsJSON(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int i = 1;

           ...do some more stuff
            
            foreach(Product p in products)
            {
                string imageTag = string.Format(@"<img src=""{0}"" alt=""""></li>", WebUtil.ImageUrl(p.Image, false));
                
                images.Add(imageTag);
                i++;
            }

            string jsonString = images.ToJSON();
            context.Response.Write(HttpUtility.HtmlEncode(jsonString));
        }

    the toJSON is simply using the helper method outlined here:

    http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx

    Sunday, June 28, 2009 11:28 PM

All replies

  • User-627724879 posted

     I think you are missing the point of JSON a little. And I could be wrong. But it is supposed to be a format to transport the data portion in a consice format, not the actual HTML. You should then take that data and serilize it into an object on the client and apply render the HTML from it. That is why the AJAX templates are going to be included in ASP.NET AJAX 4.0, etc.

    So I would reduce your JSON to serializing the entity (image info in this case) and sending that across the wire and hydrating it on the other end and rendering it.

    There are several .NET JSON libraries, check the bottom of this page, http://json.org/

     

    JSON2 - http://www.json.org/js.html

     

    Check out Rick Strahl's blog, http://www.west-wind.com/Weblog/posts/729630.aspx

     

    Monday, June 29, 2009 9:28 AM
  • User1982844092 posted

    ok, then in that case check out this url:

    http://api.flickr.com/services/feeds/photos_public.gne?format=json

    why does it show up so nicely and with no unicode crap?  I'm using the library arleady in .NET 3.5 so why would I need another?

    Look at the description field, it's returning an html string and that's what I want to do here.  I do not want to do that in jQuery..jQuery will need to read that string as it is here for example.  I want Html tags in one of my values being returned to jQuery just like this is:

    http://sorgalla.com/projects/jcarousel/examples/dynamic_flickr_feed.html

    View source and check it out.  It's using that url above to get the data and then uses .description which includes some HTML already.


    Second, what the helll is \u003? You don't see flickr as an example returning any of those unicode characters in their JSON.

    Monday, June 29, 2009 10:17 AM
  • User1982844092 posted

    Ok, I had to HtmlEncode the string before it got converted to JSON:


    string imageTag = HttpUtility.HtmlEncode(string.Format(@"<img src=""{0}"" alt="""">", ImageUrl(p.Image, false)));

    Now what I get for example in the returned JSON is:

    [{"ImageTag":"&lt;img src=&quot;http://www.xxx.com/cat_image/253.jpg&quot; alt=&quot;&quot;&gt;"},{"ImageTag":"&lt;img src=&quot;http://www.xxx.com/cat_image/268N.jpg&quot; alt=&quot;&quot;&gt;"}]

    but I want &lt; and &gt; and &quot to show their true values in this string.  How do I do this?  Obviously Flicker was able to (See the description value): http://api.flickr.com/services/feeds/photos_public.gne?format=json


    I am encoding my context.Response like this:

    context.Response.ContentType = "text/plain";
    context.Response.Charset = Encoding.UTF8.ToString();

    Monday, June 29, 2009 10:47 AM