在asp.net中验证字符串是否为json

前端之家收集整理的这篇文章主要介绍了在asp.net中验证字符串是否为json前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法验证字符串是否为json?除了try / catch.

我正在使用ServiceStack Json Serializer,但找不到与验证相关的方法.

解决方法

可能最快最肮脏的方法是检查字符串是否以'{‘开头:
  1. public static bool IsJson(string input){
  2. input = input.Trim();
  3. return input.StartsWith("{") && input.EndsWith("}")
  4. || input.StartsWith("[") && input.EndsWith("]");
  5. }

另一个选择是您可以尝试使用JavascriptSerializer类:

  1. JavaScriptSerializer ser = new JavaScriptSerializer();
  2. SomeJSONClass = ser.Deserialize<SomeJSONClass >(json);

或者你可以看看JSON.NET:

> http://james.newtonking.com/projects/json-net.aspx
> http://james.newtonking.com/projects/json/help/index.html?topic=html/SerializingJSON.htm

猜你在找的asp.Net相关文章