PositiveGuy
PositiveGuy

Reputation: 47743

Formatting JSON in ASP.NET HttpResponse

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

Upvotes: 0

Views: 1889

Answers (3)

Masoud
Masoud

Reputation: 1

all you need to do is to use javascript eval function to get a pure HTML (XML) markup on the front end.

i.e. in a ajax call to a webservice, this can be the success handler of tha call, the service returns a complex html element:

...
success: function(msg) {$(divToBeWorkedOn).html(**eval(**msg**)**);alert(eval(msg));},
...

Upvotes: 0

TheVillageIdiot
TheVillageIdiot

Reputation: 40497

You are returning JSON array. Once parsed using eval("("+returnValue+")") it is in readily usable condition.

EDIT: This code is from jquery.json.js file:

var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
var meta = {    // table of character substitutions
        '\b': '\\b',
        '\t': '\\t',
        '\n': '\\n',
        '\f': '\\f',
        '\r': '\\r',
        '"' : '\\"',
        '\\': '\\\\'
    };

$.quoteString = function(string)
// Places quotes around a string, inteligently.
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe escape
// sequences.
{
    if (escapeable.test(string))
    {
        return '"' + string.replace(escapeable, function (a) 
        {
            var c = meta[a];
            if (typeof c === 'string') {
                return c;
            }
            c = a.charCodeAt();
            return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
        }) + '"';
    }
    return '"' + string + '"';
};

Hope this gives you some direction to go ahead.

Upvotes: 1

richardtallent
richardtallent

Reputation: 35363

\u003c is an escaped less-than character in unicode (Unicode character 0x003C).

The AJAX response is fine. When that string is written to the DOM, it will show up as a normal "<" character.

Upvotes: 1

Related Questions