Validating result of JsonConvert.DeserializeObject (think “try parse”) using JSON.Net

痞子三分冷 提交于 2019-12-10 12:58:04

问题


I have incoming messages that I need to try and parse in my own objects structure. SOme of these are well formed JSON obejcts and some are just nonsense.

I use JsonConvert.DeserializeObject<MyObject>(incmoingString); to do this. This however sometimes gives me a exception when the incoming is total garbage. Other times I get a non-complete object structure when the incoming string is kind of OK - and finally it sometimes work.

I've wrapped the conversion in a try/catch and than manually validate that I've gotten the properties I need to the deserialized result.

Is there a better way to do this?


回答1:


Json.NET supports JSON Schema. You could create a schema with all the required properties marked and validate incoming JSON against it before deserializing.



来源:https://stackoverflow.com/questions/2690189/validating-result-of-jsonconvert-deserializeobject-think-try-parse-using-jso

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