Reputation: 20234
Using
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
the following deserializes my JSON array correctly:
public List<PrintJobTranslation> Translations
and the following doesn't:
public PrintJobTranslations Translations
with
public class PrintJobTranslations : List<PrintJobTranslation>
{
public string GetTranslation(string Key, string defaultValue)
{
var translation = this.FirstOrDefault(x => x.Key == Key);
if (translation == null) return defaultValue;
return translation.Translation;
}
}
It throws the error:
System.Collections.Generic.Dictionary`2[System.String,System.Object] ist kein Wert des Typs Print.printJob.PrintJobTranslation und kann in dieser generischen Sammlung nicht verwendet werden. Parametername: value
I did not find an exact translation, but should be something like this in English:
System.Collections.Generic.Dictionary`2[System.String,System.Object] is not a value of type Print.printJob.PrintJobTranslation and cannot be used in the generic collection. Parameter name: value
How can I get the JSON to deserialize correctly?
Upvotes: 0
Views: 476
Reputation: 1063724
One way to side step this completely would be to not do that - your method looks fine as an "extension method", so it'll work like an instance method, but without actually having to subclass anything:
public static class PrintJobTranslationExtensions
{
public static string GetTranslation(
this List<PrintJobTranslation> list, string Key, string defaultValue)
{
var translation = list.FirstOrDefault(x => x.Key == Key);
if (translation == null) return defaultValue;
return translation.Translation;
}
}
and just use List<PrintJobTranslation>
in your code.
Upvotes: 2