从C#ASMX返回JSON转义字符串格式不JSON对象字符串、对象、格式、ASMX

2023-09-10 19:49:09 作者:放不下对你的爱

我的JSON被返回作为转义字符串,我想没有反斜杠,外引号的对象。我敢肯定,我错过了一个演员或简单的东西,但我会放满code在这里,以避免misinter pretation。

My JSON is returning as an escaped string, I want the object without backslashes and outer quotes. I'm sure I'm missing a cast or something simple but I'll put the full code here to avoid misinterpretation.

我试图得到一些数据,通过AJAX / JSON / ASMX Web服务的Infragistics的点燃UI电网。我有数据回来,但它不是正确的格式。我不知道如果这个问题是在我的AJAX JS或C#方法的返回类型(字符串),或JSON序列化?

I'm trying to get some data into a Infragistics Ignite UI grid via ajax/JSON/asmx web service. I've got data coming back, but it's not in the correct format. I'm not sure if the issue is in my ajax js or c# method return type (string), or the JSON serialisation?

下面就是我得到使用jQuery AJAX调用调用Web服务:

Here's how I get call the web service using jquery ajax call:

    var jqxhr = $.ajax(
                {
                    type: 'POST',
                    contentType: "application/json; charset=utf-8",
                    url: "/WebMethods/AssetWebService.asmx/GetAssets",
                    dataType: "json",
                    success: success,
                    error: function (xhr, msg) { alert(msg + '\n' + xhr.responseText); }
                });

    function success(msg) {
        var entityColumns = getColumns();
        var entityData = msg.d;
        gridSetup(entityData, entityColumns);
    }

    function gridSetup(entityData, entityColumns) {
        $("#assetGrid").igGrid(
            {
                autoGenerateColumns: false,
                autoGenerateLayouts: false,
                dataSource: entityData,
                columns: entityColumns
            }
       );
    }

Webservice的C#code得到来自实体框架对象:

Webservice c# code getting objects from entity framework:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class AssetWebService : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetAssets()
        {
            List<Asset> Assets  = new List<Asset>();

        using (var db = new CognyxMES())
        {
            Assets = db.Assets.ToList();
        }

        var results = from a
                          in Assets
                          select new
                          {
                              Id = a.Id,
                              Name = a.Name,
                              AssetTypeId = a.AssetTypeId
                          };
        return results;            
        }
    }

这是我被退回的样子,当我调试在VS2013(这将导致我的网格不显示任何数据)的JSON:

The JSON that I am getting returned looks like this when I debug in VS2013 (which causes my grid to not display any data):

"[{\"Id\":3,\"Name\":\"My Asset\",\"AssetTypeId\":1}]"

我想它看起来像这是我的工作网格的时候,我很难code这样的:

I want to it to look like this as my grid works when I hard code this in:

[{ "Id": 3, "Name": "My Asset", "AssetTypeId": 1 }]

我是新来的JSON用C#,所以我知道我可以使用字符串替换或类似的破解到位,但我一个更优雅的修复,我缺少的是什么,并解释后我。谢谢

I am new to JSON with c# so I know I could use string replace or similar to hack it into place but I'm after a more elegant fix and explanation of what I'm missing. Thanks

推荐答案

您正在返回什么的字符串,而不是 JSON 。这是你的问题。

What you are returning IS a string and not JSON. That is your problem.

您的方法看起来应该像这样。 ASP.NET处理剩下的给你。

Your method should look like so. ASP.NET handles the rest for you.

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public IEnumerable<object> GetAssets()
    {
        using (var db = new assetEntities())
        {
            var results = from a
                          in db.Assets
                          select new
                          {
                              Id = a.Id,
                              Name = a.Name,
                              AssetTypeId = a.AssetTypeId
                          };
            return results;
        }
    }

根据您的jQuery请求您指定要JSON和您的 ResponseFormat = ResponseFormat.Jsonreturned`迫使这......让你的web服务将序列化到JSON响应你。十分简单。无需手动序列化需要。

Based on your jQuery request you have specified that you want JSON and yourResponseFormat = ResponseFormat.Jsonreturned` forces this... so your webservice will serialize the response to JSON for you. Easy-peasy. No manual serializing needed.