当前位置:首页>开发>正文

如何验证json格式的正确性 java 验证逻辑表达式格式的字符串的合法性

2023-05-06 22:45:28 互联网 未知 开发

 如何验证json格式的正确性 java 验证逻辑表达式格式的字符串的合法性

如何验证json格式的正确性

1 打开json验证网站http://jsonlint.com/;


2、用nodepad 打开json文件,复制文本,或者将自己的文本复制粘贴到网站上面,点击validate;


3、上图中显示Parse error on line 1: {    title: {        tex -----^ Expecting STRING, } ,实际上,整个json都是1行,这个网站只能查看正确或者错误,并不能特别精确的显示具体的行数。 需要你用nodepad ,把格式转换好,自己细心的查找问题。

java 验证逻辑表达式格式的字符串的合法性

能力有限,写的代码可能有些麻烦,不知道还有没有漏洞了。。你看看行不
public class Main
{
    public static void main(String[] args)
    {
        String str = "A && (!B && C) || C && D &&"
        System.err.println(matched(str))  // 此时打印“完全匹配”
    }

    public static String matched(String str)
    {
        if (str.matches("[() ABCD\|&!\^]*"))  // 保证只有ABCD|&()!^和空格
        {
            if (str.matches(".*[ABCD] *[ABCD] .*"))
                return "字母重叠"
            if (str.contains("&"))  // 如果有“&”符号出现
            {
                int index = str.indexOf("&")
                while (index != -1 && index < str.length())
                {
                    if (index   1 >= str.length() || str.charAt(index   1) != &)
                        return ""&"未成对出现"
                    else
                    {
                        index  = 2
                        if (index > str.length())
                            return ""&"未成对出现"
                        index = str.indexOf("&", index)
                    }
                }
            }
            if (str.contains("|"))  // 如果有“|”符号出现
            {
                int index = str.indexOf("|")
                while (index != -1 && index < str.length())
                {
                    if (index   1 >= str.length() || str.charAt(index   1) != |)
                        return ""|"未成对出现"
                    else
                    {
                        index  = 2
                        if (index > str.length())
                            return ""|"未成对出现"
                        index = str.indexOf("|", index)
                    }
                }
            }
            return "完全匹配"
        }
        return "有未知字符存在"
    }
}

最新文章