【问题标题】:How to get the value of "name" field from the json using jsonpath expression如何使用 jsonpath 表达式从 json 中获取“name”字段的值
【发布时间】:2023-03-26 21:44:01
【问题描述】:

我想从下面的 json 中获取针对“名称”字段的值。我尝试使用工具http://jsonpathfinder.com/http://jsonpath.com/? 我正在使用http://jsonpath.herokuapp.com/ 来验证表达式路径是否正确,但它总是将我返回为不正确的表达式。

{
  "data" : {
    "PensionRetriever(Customer=ABC, typeInfo=valid)" : {
      "name" : "lifeInsurance",
      "created_at" : 1552297775384,
      "attributes" : [ {
        "id" : "4044da39-c23b-4588-b6c4-975ce02e7cb2",
        "name" : "lifeInsurance",
        "created_at" : 1552297775384
   }]
    }
  }
}

我尝试使用 $.data["PensionRetriever(Customer=ABC, typeInfo=valid)"].name ,但这似乎不正确。你能告诉我现在获取 "name" 的值,即 lifeInsurance

【问题讨论】:

  • 当您拨打$.data["PensionRetriever(Customer=ABC, typeInfo=valid)"].name 时,incorrect 是什么意思?当你调用console.log($.data) 时返回什么?
  • 它给了我“在索引 55 处找到空属性”

标签: java json jsonpath json-path-expression


【解决方案1】:

var path = $.JSONPath({data: json, keepHistory: false}); var test=path.query('$.data.PensionRetriever(Customer=ABC,typeInfo=valid).name');

【讨论】:

  • 这不起作用,因为当 javascript 看到括号时,它会尝试评估表达式,而不是将其解释为对象的属性。
  • 可以查询使用
  • @song bo,能不能详细点。
【解决方案2】:

在 JSONPath 表达式中使用单引号而不是双引号,即

$.data['PensionRetriever(Customer=ABC, typeInfo=valid)'].name

使用http://jsonpath.herokuapp.com/ 上的在线评估器,通常可靠的 Jayway 失败了,显然无法消化名称 'PensionRetriever(Customer=ABC, typeInfo=valid)'。那是Jayway的错误。但是 Goessner 成功了,返回了预期的

[    "lifeInsurance" ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2020-10-06
    • 2015-03-05
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多