简体   繁体   English

从ASP.NET WebService返回的JSON不正确

[英]JSON returned from ASP.NET WebService is inadequate

I have constructed several web services that successfully serialize my .NET types into JSON. 我构建了几个Web服务,这些服务成功地将我的.NET类型序列化为JSON。 However, I'm running into an issue getting the JSON to be fully compatible with the YUI library and the DataTable in particular. 但是,我遇到了使JSON与YUI库特别是与DataTable完全兼容的问题。

The YUI DataTable can be fully configured using JSON objects. 可以使用JSON对象完全配置YUI数据表。 I created the following struct in .NET to represent a given YUI column definition: 我在.NET中创建了以下结构来表示给定的YUI列定义:

 public struct YuiColumnDefinition{
        public string key;
        public string label;
        public bool sortable;
        public string formatter;
 }

The 'formatter' property is how you instruct the YUI table to use a custom javascript function when displaying a given column. 'formatter'属性是您如何指示YUI表在显示给定列时使用自定义javascript函数。 The problem is that, since formatter is defined as a String, ASP.NET wraps it's value in double quotes upon serialization, and YUI no longer recognizes the value as a JavaScript token: 问题在于,由于格式化程序定义为字符串,因此ASP.NET在序列化时将其值用双引号引起来,并且YUI不再将值识别为JavaScript令牌:

JSON YUI expects JSON YUI期望

[ 
{key:"1", label:"Order Date", formatter:myCustomJavaScriptFunction, sortable:true},
{key:"2", label:"Customer Name", formatter:null, sortable:false}
]

JSON ASP.NET creates JSON ASP.NET创建

[ 
{key:"1", label:"Order Date", formatter:"myCustomJavaScriptFunction", sortable:true},
{key:"2", label:"Customer Name", formatter:null, sortable:false}
]

Anyone have a solution outside of modifying the YUI source code? 除了修改YUI源代码以外,还有其他解决方案吗?

Thanks 谢谢

Change your parsing code. 更改您的解析代码。

Think of json as a replacement for xml, you wouldnt put a variable/function in xml. 将json视为xml的替代品,您不会在xml中放置变量/函数。 In either, you could easily identify the name or type (say from a list or enum) of the formatter to use. 在任何一种格式中,您都可以轻松确定要使用的格式化程序的名称或类型(例如从列表或枚举中获取)。 Then your parsing code would know that it should assign a variable/method as the "formatter" property. 然后,您的解析代码将知道它应该将变量/方法分配为“格式器”属性。

Its just incorrect to return an actual variable/function in a callback like that. 在这样的回调中返回实际的变量/函数是不正确的。 You could make it work but honestly its not the way to go. 可以使它起作用,但老实说这不是可行的方法。

I would do the following... 我会做以下...

Change your return json to this. 将您的返回json更改为此。

[ 
{key:"1", label:"Order Date", formatterName:"myCustomJavaScriptFunction", sortable:true},
{key:"2", label:"Customer Name", formatterName:null, sortable:false}
]

Then in JS, lets assume that json is stored in variable returnedObj 然后在JS中,假设json存储在returnedObj变量中

function augmentReturnedObject(returnedObj)
{
    // validate that returnObj.formatterName (as a variable) is not undefined
    var isValidObj = (window[returnedObj.formatterName] !== undefined);

    if (isValidObj)
    {

        // this will return the actual function / variable, here we are assigning it to returnedObj.formatter
        returnedObj.formatter = window[returnedObj.formatterName];
    }
    else
    {
        returnedObj.formatter = null;
    }
}

You could easily reduce that down to this without much thought 您可以轻松地将其减少为

function augmentReturnedObject(returnedObj)
{
    var specifiedMethod = window[returnedObj.formatterName];

    returnedObj.formatter = (specifiedMethod === undefined) ? null : window[returnedObj.formatterName];
}

So in the end you'd take your json object, and do augmentReturnedObject(returnedObj); 因此,最后,您将使用json对象,并进行augmentReturnedObject(returnedObj); and at that point you can pass returnedObj to YUI 而在这一点上,你可以通过returnedObj到YUI

As Allen correctly pointed out: 正如艾伦正确指出的那样:

Its just incorrect to return an actual variable/function in a callback like that. 在这样的回调中返回实际的变量/函数是不正确的。

Still, I couldn't find anywhere in the YUI documentation that spells out how to deal with javascript functions returned as strings from either JSON or XML. 不过,我在YUI文档中找不到任何地方可以说明如何处理以JSON或XML字符串形式返回的javascript函数。

Thank goodness for blogs. 谢天谢地,博客。 As pointed out in this one, the "proper" way to register custom javascript formatting functions is by using YAHOO.widget.DataTable.Formatter: 本节所述,注册自定义javascript格式设置功能的“正确”方法是使用YAHOO.widget.DataTable.Formatter:

JSON Column Definitions as returned from .ASMX 从.ASMX返回的JSON列定义

[              
 {key:"1", label:"Order Date", formatter:"myCustomJavaScriptFunction1", sortable:true},             
 {key:"2", label:"Customer Name", formatter:null, sortable:false}             
 {key:"3", label:"State", formatter:"myCustomJavaScriptFunction2", sortable:false}             
]

Javscript to wire up the YUI DataTable Javscript连接YUI数据表

YAHOO.widget.DataTable.Formatter.myCustomJavaScriptFunction1= this.myCustomJavaScriptFunction1;
YAHOO.widget.DataTable.Formatter.myCustomJavaScriptFunction2= this.myCustomJavaScriptFunction2;

function  myCustomJavaScriptFunction1(elCell, oRecord, oColumn, oData) {
   //do something
}

function  myCustomJavaScriptFunction2(elCell, oRecord, oColumn, oData){
   //do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM