J'ai un code qui renvoie le résultat de LINQ (j'ai même essayé d'ajouter ToList()
)JSON sérialisation LINQ résultat en C#
Je vois la collection OK
mais quand je sors de la corps de la fonction, une autre couche d'abstraction est ajouté
à la fin ELorsque je retourne au client le résultat
il obtient:
« {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 = réussi} "
Comment est-ce que je peux sérialiser le résultat linq correctement?
Mise à jour Auparavant, il travaillait sans appeler 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;
}
}
et
[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 });
}
}
pour une raison étrange le code RECONNAÎTRE JsonResult mais pas la fonction JSON(). Pourquoi? –
@EladBenda c'est une méthode de la classe de base 'Controller' à partir de laquelle votre classe de contrôleur doit hériter: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.json(v= vs.98) .aspx – Jan
stange, il a l'habitude de fonctionner sans appel 'JSON()'. vois-moi mettre à jour. –