反序列化JSON,有时值是一个数组,有时是“". (空白字符串) [英] Deserialize JSON, sometimes value is an array, sometimes "" (blank string)
问题描述
我正在尝试反序列化一个字段:
I am trying to deserialize a field:
"presenters":[{...},{...}]
但是某些行仅返回:
"presenters":""
当序列化程序到达具有该空字符串的行时,我得到:
When the serializer gets to the row with that empty string I get:
将值"转换为类型"System.Collections.Generic.List`1 [DataPrototype.Model.Presenter]"时出错.
Error converting value "" to type 'System.Collections.Generic.List`1[DataPrototype.Model.Presenter]'.
我是不是在想我需要一个将空字符串更改为空List的JsonConverter吗?
Am I right in thinking that I need a JsonConverter that will change the empty string into an empty List?
推荐答案
是.
在JsonConverter内部测试来自JsonReader的令牌类型.
Inside the JsonConverter test the token type from the JsonReader.
如果是字符串,则返回null.
If it is a string then return null.
如果它是数组的开始,则使用传递给converter方法的JsonReader和JsonSerializer对数组进行反序列化.
If it is the start of an array then use the JsonReader and JsonSerializer passed to the converter method to deserialize the array.
这篇关于反序列化JSON,有时值是一个数组,有时是“". (空白字符串)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!