【问题标题】:Problem filtering jsonpath-plus result set过滤jsonpath-plus结果集的问题
【发布时间】:2021-06-21 20:14:09
【问题描述】:

使用 jsonpath-plus 模块(打字稿),我试图导航到 json 文档中的特定对象。目标对象向下几级,包括通过 2 级数组。以下jsonpath语句:

$..['gmd:DQ_AbsoluteExternalPositionalAccuracy']

在线jsonpath测试网站jsonpath.com返回如下结果:

[
  {
    "gmd:nameOfMeasure": {
      "gco:CharacterString": "Difference to ICESat LE90"
    },
    "gmd:result": {
      "gmd:DQ_QuantitativeResult": {
        "gmd:valueUnit": {
          "@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
        },
        "gmd:value": {
          "gco:Record": {
            "gco:Real": "0.655608"
          }
        }
      }
    }
  },
  {
    "gmd:nameOfMeasure": {
      "gco:CharacterString": "Difference to ICESat Vertical Mean"
    },
    "gmd:result": {
      "gmd:DQ_QuantitativeResult": {
        "gmd:valueUnit": {
          "@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
        },
        "gmd:value": {
          "gco:Record": {
            "gco:Real": "-0.005536"
          }
        }
      }
    }
  },
  {
    "gmd:nameOfMeasure": {
      "gco:CharacterString": "Difference to ICESat Vertical RMSE"
    },
    "gmd:result": {
      "gmd:DQ_QuantitativeResult": {
        "gmd:valueUnit": {
          "@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
        },
        "gmd:value": {
          "gco:Record": {
            "gco:Real": "0.398874"
          }
        }
      }
    }
  },
  {
    "gmd:nameOfMeasure": {
      "gco:CharacterString": "Difference to GCP LE90"
    },
    "gmd:result": {
      "gmd:DQ_QuantitativeResult": {
        "gmd:valueUnit": {
          "@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
        },
        "gmd:value": {
          "gco:Record": {
            "gco:Real": "2.897789"
          }
        }
      }
    }
  },
  {
    "gmd:nameOfMeasure": {
      "gco:CharacterString": "Difference to GCP Vertical Mean"
    },
    "gmd:result": {
      "gmd:DQ_QuantitativeResult": {
        "gmd:valueUnit": {
          "@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
        },
        "gmd:value": {
          "gco:Record": {
            "gco:Real": "-0.383740"
          }
        }
      }
    }
  },
  {
    "gmd:nameOfMeasure": {
      "gco:CharacterString": "Difference to GCP Vertical RMSE"
    },
    "gmd:result": {
      "gmd:DQ_QuantitativeResult": {
        "gmd:valueUnit": {
          "@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
        },
        "gmd:value": {
          "gco:Record": {
            "gco:Real": "1.760134"
          }
        }
      }
    }
  },
  {
    "gmd:nameOfMeasure": {
      "gco:CharacterString": "Absolute horizontal accuracy CE90"
    },
    "gmd:result": {
      "gmd:DQ_QuantitativeResult": {
        "gmd:valueUnit": {
          "@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
        },
        "gmd:value": {
          "gco:Record": {
            "gco:Real": "10"
          }
        }
      }
    }
  },
  {
    "gmd:nameOfMeasure": {
      "gco:CharacterString": "Absolute vertical accuracy LE90"
    },
    "gmd:result": {
      "gmd:DQ_QuantitativeResult": {
        "gmd:valueUnit": {
          "@xlink:href": "http://www.opengis.net/def/uom/OGC/1.0/metre"
        },
        "gmd:value": {
          "gco:Record": {
            "gco:Real": "10"
          }
        }
      }
    }
  }
]

在这个例子中我需要检索的对象是最后一个对象(包含字符串“Absolute vertical accuracy LE90”),但我不能指望它总是在同一个位置。我试图通过附加来过滤这个结果集

[?(@['gmd:nameOfMeasure']['gco:CharacterString']=="Absolute vertical accuracy LE90")]

到原来的 jsonpath 表达式(产生新的表达式

$..['gmd:DQ_AbsoluteExternalPositionalAccuracy'][?(@['gmd:nameOfMeasure']['gco:CharacterString']=="Absolute vertical accuracy LE90")]

但是过滤器语句对结果没有影响。只是为了好玩,我在 https://jsonpath.herokuapp.com/ 的 java 的 Jayway jsonpath 实现中尝试了相同的表达式,并成功地将结果过滤到一个所需的对象。

谁能告诉我如何使用 jsonpath-plus 正确过滤这个结果集?

【问题讨论】:

    标签: json typescript object filter jsonpath


    【解决方案1】:

    使用

    查询您的示例 JSON
    $[?(@['gmd:nameOfMeasure']['gco:CharacterString']==="Absolute vertical accuracy LE90")]
    

    为我工作;也许您可以简单地分两步查询。

    由于只提供了内部 JSON,我只能猜测在查询完整 JSON 时出了什么问题。需要注意的一件事:JsonPath-Plus 期望 JSON 包含在方括号中:

    [
      {"gmd:DQ_AbsoluteExternalPositionalAccuracy": [
        ...inner json      
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多