Art*_*yom 57
非常简单:
我在谈论非空间数据.
Run*_* FS 33
public static bool IsJson(this string input){
input = input.Trim();
return input.StartsWith("{") && input.EndsWith("}")
|| input.StartsWith("[") && input.EndsWith("]");
}
Run Code Online (Sandbox Code Playgroud)
它有点脏,但简单快捷
它基本上足以测试第一个角色.测试最后一个是测试良好形成的一种非常粗鲁的方法.它并不能保证它只是提高了它形成良好的可能性.
如果你想要一个更强大的版本,你可以利用if的短路来仅评估良好的形状,如果初始检查是好的.以下代码依赖于JSON.net
public static bool IsJson(this string input){
input = input.Trim();
Predicate IsWellFormed = () => {
try {
JToken.Parse(input);
} catch {
return false;
}
return true;
}
return (input.StartsWith("{") && input.EndsWith("}")
|| input.StartsWith("[") && input.EndsWith("]"))
&& IsWellFormed()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28422 次 |
| 最近记录: |