-1

我有一些返回 linq 结果的代码(我什至尝试添加ToList()

我看合集OK

在此处输入图像描述

但是当我跳出函数体时,又增加了一个抽象层

在此处输入图像描述

最后,当我向客户返回结果时

它得到:

"{ 应用程序 = System.Collections.Generic.List 1[\u003c\u003ef__AnonymousType14[System.String,System.String,\u003c\u003ef__AnonymousType0`4[System.String,System.String,System.String,System.String],System.Object]] , 状态 = 成功 }"

如何正确序列化 linq 结果?

事先更新 ,它曾经在不调用的情况下工作JSON()

    public object GetAppsData()
    {

        var appsData = new List<AppData>();
        using (IDataReader dr = DatabaseFactory.CreateDatabase().ExecuteReader("usp_AppsData_GetAll"))
        {

            while (dr.Read())
            {
                appsData.Add(new AppData()
                {
                    AppGuid = (Guid)dr["AppGuid"],
                    AppName = (string)dr["AppName"],
                    ClientAppID = dr["ClientAppID"] == DBNull.Value ? null : (string)dr["ClientAppID"],
                    Url = dr["Url"] == DBNull.Value ? null : (string)dr["Url"],
                    DisplayName = dr["DisplayName"] == DBNull.Value ? null : (string)dr["DisplayName"],
                    AppDesc = dr["AppDesc"] == DBNull.Value ? null : (string)dr["AppDesc"],
                    PrivacyPolicyUrl = dr["PrivacyPolicyUrl"] == DBNull.Value ? null : (string)dr["PrivacyPolicyUrl"],
                    TermsOfUseUrl = dr["TermsOfUseUrl"] == DBNull.Value ? null : (string)dr["TermsOfUseUrl"],
                    //Platform = dr["Platform"] == DBNull.Value ? null : (string)dr["Platform"],
                    //MaxVersion = dr["MaxVersion"] == DBNull.Value ? null : (string)dr["MaxVersion"],
                    LocalizationKey = dr["LocalizationKey"] == DBNull.Value ? null : (string)dr["LocalizationKey"],
                    Compatibility = dr["Compatibility"] == DBNull.Value ? null : jss.Deserialize<object>((string)dr["Compatibility"])

                });

            }
        }

        var appsDataJson = appsData.Select(GenerateAppsDataClientResponse);

        return new { apps = appsDataJson, Status = "succeeded" };
    }



    private object GenerateAppsDataClientResponse(AppData a)
    {
        object result;
        if (a.Compatibility == null)
        {
            result = new
            {
                id = a.ClientAppID,
                url = a.Url,
                optionsDialog = new
                {
                    displayName = a.DisplayName,
                    appDesc = a.AppDesc,
                    privacyPolicyUrl = a.PrivacyPolicyUrl,
                    termsOfUseUrl = a.TermsOfUseUrl
                }
            };
        }
        else
        {
            // this line throws NullReferenceException
            result = new
            {
                id = a.ClientAppID,
                url = a.Url,
                optionsDialog = new
                {
                    displayName = a.DisplayName,
                    appDesc = a.AppDesc,
                    privacyPolicyUrl = a.PrivacyPolicyUrl,
                    termsOfUseUrl = a.TermsOfUseUrl
                },
                compatibility = a.Compatibility
            };
        }
        return result;
    }
}

    [HttpGet]
    public ActionResult GetAppsData()
    {
        try
        {
           AppsDataManager appsData = new AppsDataManager();
           object adr =  appsData.GetAppsData();
           return this.JsonpOptional(adr);
        }
        catch (Exception ex)
        {
            Log.Application.Error("ClientDataController.GetSettings", ex);
            return this.JsonpOptional(new { Status = "failed", Reason = ex.Message });
        }
    }
4

1 回答 1

2

new您在调试器中看到的神秘数据类型只是您使用关键字返回的匿名类的类名。

您的主要问题是,您必须返回 aJsonResult而不是普通对象。并且不要忘记指定JsonRequestBehavior.AllowGet. 否则在将 JSON 数据返回到 HTTP GET 请求时会出现异常:

return Json(new { apps = appsDataJson, Status = "succeeded" },
                 JsonRequestBehavior.AllowGet);
于 2012-11-14T12:18:54.560 回答