Validate a string to be json or not in asp.net

后端 未结 3 1130
长情又很酷
长情又很酷 2021-01-05 10:49

is there any way to validate a string to be json or not ? other than try/catch .

I\'m using ServiceStack Json Serializer and couldn\'t find a method related to valid

3条回答
  •  孤独总比滥情好
    2021-01-05 11:23

    A working code snippet

    public bool isValidJSON(String json)
    {
        try
        {
            JToken token = JObject.Parse(json);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    

    Source

提交回复
热议问题