json序列化linq结果c#

时间:2012-11-14 12:16:50

标签: c# asp.net .net json

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

我看到收藏品好了

enter image description here

但是当我离开功能体时, 添加另一个抽象层

enter image description here

最后我返回客户端的结果

它得到:

“{apps = System.Collections.Generic.List 1[\u003c\u003ef__AnonymousType1 4 [System.String,System.String,\ u003c \ u003ef__AnonymousType0`4 [System.String,System.String,System.String,System。 String],System.Object]],Status = succeeded}“

如何正确序列化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 });
        }
    }

1 个答案:

答案 0 :(得分:2)

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

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

return Json(new { apps = appsDataJson, Status = "succeeded" },
                 JsonRequestBehavior.AllowGet);