我有一些返回 linq 结果的代码(我什至尝试添加ToList()
)
我看合集OK
但是当我跳出函数体时,又增加了一个抽象层
最后,当我向客户返回结果时
它得到:
"{ 应用程序 = 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]] , 状态 = 成功 }"
如何正确序列化 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 });
}
}