登录
首页 >  Golang >  Go问答

遇到使用Go JSONPath解析JSON时的困扰问题

来源:stackoverflow

时间:2024-02-14 13:03:23 162浏览 收藏

你在学习Golang相关的知识吗?本文《遇到使用Go JSONPath解析JSON时的困扰问题》,主要介绍的内容就涉及到,如果你想提升自己的开发能力,就不要错过这篇文章,大家要知道编程理论基础和实战操作都是不可或缺的哦!

问题内容

我使用代码将 json 数据从 yarn rest api 转换为 prometheus 数据类型: https://github.com/prometheus-community/json_exporter。 但是,它打印了错误:

level=error ts=2021-07-08t06:31:03.712z caller=collector.go:83 msg="无法提取指标值" path={.capacity} err="未找到容量" metric="desc {fqname:“queues_capacity”,help:“有关队列的信息”,constlabels:{},variablelabels:[type]}”

我想知道我的 yaml 文件配置是否有问题(例如嵌套 json),或者只是代码原因。

我的 yaml 配置是:

metrics:
- name: queues
  path: "{ .scheduler.schedulerinfo.queues.queue }"
  help: information on queues
  type: object
  labels:
       type: '{.type}'
  values:
       capacity: '{.capacity}'

json 文件的一部分是:

{
"scheduler": {
"schedulerInfo": {
"type": "capacityScheduler",
"capacity": 100,
"usedCapacity": 1.0526316,
"maxCapacity": 100,
"queueName": "root",
"queues": {
"queue": [
{
"type": "capacitySchedulerLeafQueueInfo",
"capacity": 10,
"usedCapacity": 10.526316,
"maxCapacity": 100,

正确答案


使用[*]首先获取所有对象,所以它应该是:

path: "{ .scheduler.schedulerInfo.queues.queue[*] }"

根据:https://kubernetes.io/docs/reference/kubectl/jsonpath/

理论要掌握,实操不能落!以上关于《遇到使用Go JSONPath解析JSON时的困扰问题》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>