Deserialize JSON, sometimes value is an array, sometimes “” (blank string)

為{幸葍}努か 提交于 2019-12-08 11:17:15

问题


I am trying to deserialize a field:

"presenters":[{...},{...}]

but some of the rows come back with only:

"presenters":""

When the serializer gets to the row with that empty string I get:

Error converting value "" to type 'System.Collections.Generic.List`1[DataPrototype.Model.Presenter]'.

Am I right in thinking that I need a JsonConverter that will change the empty string into an empty List?


回答1:


Yes.

Inside the JsonConverter test the token type from the JsonReader.

If it is a string then return null.

If it is the start of an array then use the JsonReader and JsonSerializer passed to the converter method to deserialize the array.



来源:https://stackoverflow.com/questions/4572732/deserialize-json-sometimes-value-is-an-array-sometimes-blank-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!