如何确保该字符串使用JSON.NET有效的JSON [英] How to make sure that string is Valid JSON using JSON.NET

查看:101
本文介绍了如何确保该字符串使用JSON.NET有效的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个原始字符串。我只是想验证字符串是否为有效的JSON或没有。我使用JSON.NET。

I have a raw string. I just want to validate whether the string is valid JSON or not. I'm using JSON.NET.

推荐答案

通过code:

您最好的选择是失败的解析情况下,使用的try-catch 和捕获异常中解析。的(我不知道有任何的TryParse 的方法)的。

Your best bet is to use parse inside a try-catch and catch exception in case of failed parsing. (I am not aware of any TryParse method).

(使用JSON.Net)

(Using JSON.Net)

简单的方法是到解析使用字符串的 JToken.Parse ,并且还要检查字符串 {或 [} 结束] 分别为(根据该相加<一href=\"http://stackoverflow.com/questions/11835593/validate-a-string-to-be-json-or-not-in-asp-net\">answer):

Simplest way would be to Parse the string using JToken.Parse, and also to check if the string starts with { or [ and ends with } or ] respectively (added from this answer):

private static bool IsValidJson(string strInput)
{
    strInput = strInput.Trim();
    if ((strInput.StartsWith("{") && strInput.EndsWith("}")) || //For object
        (strInput.StartsWith("[") && strInput.EndsWith("]"))) //For array
    {
        try
        {
            var obj = JToken.Parse(strInput);
            return true;
        }
        catch (JsonReaderException jex)
        {
            //Exception in parsing json
            Console.WriteLine(jex.Message);
            return false;
        }
        catch (Exception ex) //some other exception
        {
            Console.WriteLine(ex.ToString());
            return false;
        }
    }
    else
    {
        return false;
    }
}

导入检查 { [等是基于一个事实,即<$原因C $ C> JToken.Parse 将解析值,如1234'串'。作为一个有效的标记。另一种选择可能是既 JObject.Parse JArray.Parse 解析中使用,并看看是否有任何人成功,但我相信检查 {} [] 应该更容易。(感谢@RhinoDevel的<一个href=\"http://stackoverflow.com/questions/14977848/how-to-make-sure-that-string-is-valid-json-using-json-net/14977915#comment55540771_14977915\">pointing出来)

The reason to add checks for { or [ etc was based on the fact that JToken.Parse would parse the values such as "1234" or "'a string'" as a valid token. The other option could be to use both JObject.Parse and JArray.Parse in parsing and see if anyone of them succeeds, but I believe checking for {} and [] should be easier. (Thanks @RhinoDevel for pointing it out)

无JSON.Net

Without JSON.Net

您可以利用Net框架4.5 System.Json命名空间,如:

You can utilize .Net framework 4.5 System.Json namespace ,like:

string jsonString = "someString";
try
{
    var tmpObj = JsonValue.Parse(jsonString);
}
catch (FormatException fex)
{
    //Invalid json format
    Console.WriteLine(fex);
}
catch (Exception ex) //some other exception
{
    Console.WriteLine(ex.ToString());
}

(但是,必须使用命令通过的NuGet包管理器安装 System.Json PM&GT;安装封装System.Json -Version 4.0.20126.16343 的软件包管理器控制台)的(从<一所href=\"http://stackoverflow.com/questions/12859319/json-does-not-exist-in-the-namespace-system\">here)

(But, you have to install System.Json through Nuget package manager using command: PM> Install-Package System.Json -Version 4.0.20126.16343 on Package Manager Console) (taken from here)

非code方式:

通常,当有一个小JSON字符串,你正在努力寻找在JSON字符串错误,那么我的亲自的preFER使用可用的在线工具。我最常做的是:

Usually, when there is a small json string and you are trying to find a mistake in the json string, then I personally prefer to use available on-line tools. What I usually do is:

这篇关于如何确保该字符串使用JSON.NET有效的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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