Zaimplementowałem metodę zwraca List<string>
zgodnie z ciągiem json.deserializować puste ciąg do listy <string>
Działa dobrze, zdałem sobie sprawę, że próbuję deserializować pusty ciąg. To się nie psuje ani nie podnosi wyjątku. Zwraca wartość null
zamiast pustego List<string>
.
Pytanie, czy mogę dotknąć, aby zwrócić mi pustą wartość List<string>
zamiast wartości null
?
return JsonConvert.DeserializeObject(content, typeof(List<string>));
EDIT metoda Generic:
public object Deserialize(string content, Type type) {
if (type.GetType() == typeof(Object))
return (Object)content;
if (type.Equals(typeof(String)))
return content;
try
{
return JsonConvert.DeserializeObject(content, type);
}
catch (IOException e) {
throw new ApiException(HttpStatusCode.InternalServerError, e.Message);
}
}
'type.GetType()' jest nieprawidłowe; da konkretny typ dziedzicząc po 'System.Type', który nie jest tym, czego potrzebujesz. Chcesz 'if (type == typeof (Object)) tam. W następnym 'if' możesz również użyć' == '(dla spójności). –