验证JsonConvert.DeserializeObject的结果(认为"尝试解析")使用JSON.Net [英] Validating result of JsonConvert.DeserializeObject (think "try parse") using JSON.Net

查看:908
本文介绍了验证JsonConvert.DeserializeObject的结果(认为"尝试解析")使用JSON.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要尝试并以我个人的对象结构分析传入的消息。其中也不乏一些合式JSON obejcts有的只是无稽之谈。

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.

我用 JsonConvert.DeserializeObject<&MyObject的GT;(incmoingString); 来做到这一点。然而,这有时给了我一个异常时,传入的垃圾总量。其他时候,我得到一个不完整的对象结构当输入字符串是一种正常的 - 终于有时工作

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.

我裹转换在try /捕捉和比手动验证我已经得到我需要反序列化的结果的属性。

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?

推荐答案

Json.NET支持的 JSON模式。与所有必需的属性标记您可以创建一个架构和反序列化之前验证其对传入的JSON

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

这篇关于验证JsonConvert.DeserializeObject的结果(认为&QUOT;尝试解析&QUOT;)使用JSON.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆