需要一个JSON字符串验证 [英] Need a string JSON Validator

查看:196
本文介绍了需要一个JSON字符串验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用newtonsoft的JSON.Net和爱的每一分钟。

I'm using newtonsoft's JSON.Net and loving every minute of it.

不过,我使用 JObject.Parse(jsonString)抢JToken从响应字符串。如果我把无效的JSON,我得到一个异常。我可以捕获异常,但我想,能够把它发送到解析之前,首先测试的字符串。

However, I am using JObject.Parse(jsonString) to grab a JToken from a response string. If I send invalid JSON, I get an exception. I can catch the exception, but I would like to, be able to test the string first before sending it to Parse.

也许一个类似于 JObject.TryParse()(不存在)。

Maybe something akin to JObject.TryParse() (which doesn't exist).

我更兼   布尔ValidJson(串)

I'd even take bool ValidJson(string)

我知道有JSONLint在那里,但我真的想保持外部话费降到最低点。

I know there's JSONLint out there, but I would really like to keep the external calls to a minimum.

任何想法?

推荐答案

最简单的解决办法是写一个函数,它们调用 JObject.Parse ,如果它抛出返回false该异常。

The simplest solution would be to write a function which calls JObject.Parse and returns false if it throws the exception.

这篇关于需要一个JSON字符串验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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