如何检查给定的 JSON 路径表达式是否在语法上正确?

huangapple go评论64阅读模式
英文:

How to check a given json-path expression is syntactically correct?

问题

我需要验证以下 JSON 路径是否在语法上正确或不正确:

$[*].key1.key2[*].key3.key4 // 有效
$[*].key1/key2[*].key3"key4 // 无效

是否有可以在 Java 中检查上述 JSON 路径表达式并返回 true/false 的 API?

英文:

I need to validate whether the below json-paths are syntactically correct or not:

$[*].key1.key2[*].key3.key4 // valid
$[*].key1/key2[*].key3"key4 // invalid

Is there any API which can check the above json-path expressions and return true/false in Java ?

答案1

得分: 1

尝试导入 Jayway JsonPath https://github.com/json-path/JsonPath,然后使用 JsonPath.read("{}", yourJsonPath);,如果没有出错,路径就是有效的。

英文:

Try importing Jayway JsonPath <https://github.com/json-path/JsonPath> then JsonPath.read("{}", yourJsonPath); and if it doesn't explode the path is valid.

答案2

得分: -1

使用类似JSON.parse的JSON解析器:

function IsJsonString(str) 
{
    try 
    {
        JSON.parse(str);
    }
    catch (e) 
    {
        return false;
    }
    return true;
}
英文:

Use a JSON parser like JSON.parse:

function IsJsonString(str) 
  {
      try 
      {
        JSON.parse(str);
      }
      catch (e) 
      {
        return false;
      }
    return true;
  }

huangapple
  • 本文由 发表于 2020年8月11日 19:28:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63357218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定