【问题标题】:Formatting JSON in ASP.NET HttpResponse在 ASP.NET HttpResponse 中格式化 JSON
【发布时间】:2009-06-29 03:29:41
【问题描述】:

我在 .ashx 响应中通过 JSON 发回一堆图像标签。

我不确定如何格式化,以便字符串返回真实标签。我尝试了 HtmlEncode 并修复了它,但后来我得到了这个愚蠢的 \u003c 废话:

["\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"]

\u003c 到底是什么?

这是我创建 JSON 以响应我的 .ashx 的代码:

private void GetProductsJSON(HttpContext 上下文) { context.Response.ContentType = "文本/纯文本"; int i = 1;

...做更多的事情

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));

}

toJSON 只是使用此处概述的辅助方法:

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

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    \u003c 是 unicode 中的转义小于字符(Unicode 字符 0x003C)。

    AJAX 响应很好。当该字符串写入 DOM 时,它将显示为普通的“

    【讨论】:

    • 但是为什么这不显示任何奇怪的Unicode字符:api.flickr.com/services/feeds/photos_public.gne?format=json
    • 请注意描述如何具有有效的 html 而没有一个 \u00
    • 好的,那么在这种情况下,请查看以下网址:api.flickr.com/services/feeds/photos_public.gne?format=json 为什么它显示得这么好并且没有 unicode 废话?查看源代码并检查一下。它使用上面的 url 来获取数据,然后使用 .description 已经包含一些 HTML。
    • 在将纯 JavaScript 响应发送回浏览器时(正如 Flickr API 所做的那样),您没有必须转义 。但是,如果您调用 toJSON() 在 HTML 页面中的
    【解决方案2】:

    您正在返回 JSON 数组。一旦使用 eval("("+returnValue+")") 解析,它就处于易于使用的状态。

    编辑:此代码来自jquery.json.js 文件:

    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 + '"';
    };
    

    希望这能给你一些前进的方向。

    【讨论】:

    • 所以你说有 \u003c 是正常的,eval 会将其转换为 ?
    • 我还是不明白 \u003c 是什么
    • 我试过(返回包含 img 标签数组的数组。但是当我得到正确的 HTML { var ar=response.d;} 并且 ar 包含图像数组
    • 谢谢,所以 jQuery 解析器会自动转换它们。
    【解决方案3】:

    您需要做的就是使用 javascript eval 函数在前端获取纯 HTML (XML) 标记。

    即在对 web 服务的 ajax 调用中,这可以是调用的成功处理程序, 该服务返回一个复杂的 html 元素:

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

    【讨论】:

      猜你喜欢
      • 2018-04-26
      • 1970-01-01
      • 2012-09-23
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      相关资源
      最近更新 更多