java - 覆盖 json 的现有属性,或者如果 json 中不存在则添加 next

标签 java json jsonpath

我有一些 JSON,在 main 方法中声明为字符串用于测试目的。 我的目标是将 JSONPATH 与 DocumentContext 结合使用,如果属性存在则覆盖属性,如果属性不存在则添加它。 有可能实现这一目标吗? 我的 JSON

How to add new node to Json using JsonPath? Convert a JSON object to another JSON object in Java 我访问过这两个,但我的结果不是 JSON,而是“com.jayway.jsonpath.internal.JsonReader@1ae369b7”

{
  "meta": {
    "drilldownEnabled": false
  },
  "chart": {
  renderTo:"container",
    "additionalData": {
      "dateTime": false,
      "datetype": "string",
      "cliccable": true,
      "drillable": false,
      "drillableChart": false,
      "isCockpit": true,
      "categoryColumn": "periodo_analisi",
      "categoryGroupBy": "",
      "categoryGroupByNamens": "",
      "categoryName": "periodo_analisi",
      "categoryOrderColumn": "",
      "categoryOrderType": "",
      "categoryStacked": "",
      "categoryStackedType": ""
    },
    "zoomType": "xy",
    "panning": true,
    "type": "column",
    "options3d": {
      "enabled": false,
      "alpha": 25,
      "beta": 15,
      "depth": 50,
      "viewDistance": 25
    },
    "backgroundColor": "#FFFFFF",
    "heightDimType": "pixels",
    "widthDimType": "pixels",
    "plotBackgroundColor": null,
    "plotBorderWidth": null,
    "plotShadow": false,
    "borderColor": "#FFFFFF",
    "style": {
      "backgroundColor": "#FFFFFF",
      "fontFamily": "",
      "fontWeight": "",
      "fontSize": ""
    },
    "events": {}
  },
  "colors": [
    "#ff5722"
  ],
  "title": {
    "text": "",
    "style": {
      "align": "",
      "color": "",
      "fontFamily": "",
      "fontSize": "",
      "fontWeight": ""
    }
  },
  "legend": {
    "enabled": false
  },
  "xAxis": [
    {
      "plotBands": [
        {
          "label": {
            "text": "",
            "align": "center"
          },
          "color": "",
          "from": 0,
          "to": 0
        }
      ],
      "plotLines": [
        {
          "label": {
            "text": "",
            "align": "center"
          },
          "color": "",
          "dashStyle": "",
          "value": 0,
          "width": 0
        }
      ],
      "type": "category",
      "id": 0,
      "title": {
        "customTitle": false,
        "text": "periodo_analisi",
        "style": {}
      },
      "labels": {
        "style": {
          "color": "",
          "fontFamily": "",
          "fontSize": "",
          "fontWeight": ""
        },
        "align": ""
      }
    }
  ],
  "yAxis": [
    {
      "plotBands": [
        {
          "label": {
            "text": "",
            "align": "center"
          },
          "color": "",
          "from": 0,
          "to": 0
        }
      ],
      "plotLines": [
        {
          "label": {
            "text": "",
            "align": "center"
          },
          "color": "",
          "dashStyle": "",
          "value": 0,
          "width": 0,
          "zIndex": 1
        }
      ],
      "title": {
        "text": "NUM_GG_GIACENZA_AVG",
        "customTitle": false,
        "style": {
          "color": "",
          "fontFamily": "",
          "fontWeight": "",
          "fontSize": ""
        }
      },
      "labels": {
        "style": {
          "color": "",
          "fontFamily": "",
          "fontSize": "",
          "fontWeight": ""
        },
        "align": ""
      },
      "gridLineDashStyle": "$convertedTypeline",
      "minorGridLineDashStyle": "$convertedMinorTpeline"
    }
  ],
  "series": [
    {
      "name": "NUM_GG_GIACENZA_AVG",
      "dataLabels": {
        "style": {
          "color": "",
          "fontFamily": "",
          "fontWeight": "",
          "fontSize": "",
          "fontStyle": ""
        },
        "enabled": true,
        "labelFormat": "{y:,.2f}"
      },
      "data": [
        {
          "drilldown": false,
          "y": 32.6667,
          "name": "Q1-2019",
          "datetype": "string"
        },
        {
          "drilldown": false,
          "y": 29,
          "name": "Q3-2018",
          "datetype": "string"
        },
        {
          "drilldown": false,
          "y": 134.5,
          "name": "Q4-2018",
          "datetype": "string",
          "color": "#F10AE8"
        }
      ],
      "selected": true,
      "tooltip": {
        "valueDecimals": 2,
        "scaleFactor": "empty",
        "ttBackColor": "#FCFFC5"
      },
      "yAxis": 0
    }
  ],
  "tooltip": {
    "borderWidth": 0,
    "borderRadius": 0,
    "followTouchMove": false,
    "followPointer": true,
    "useHTML": true,
    "backgroundColor": null,
    "style": {
      "padding": 0
    }
  },
  "lang": {
    "noData": ""
  },
  "noData": {
    "style": {
      "fontFamily": "",
      "fontSize": "",
      "color": ""
    },
    "position": {
      "align": "center",
      "verticalAlign": "middle"
    }
  },
  "credits": {
    "enabled": false
  },
  "plotOptions": {
    "line": {
      "marker": {
        "symbol": "circle",
        "lineWidth": 2
      }
    },
    "column": {},
    "bar": {},
    "series": {
      "cursor": "pointer",
      "point": {
        "events": {}
      },
      "dataLabels": {
        "allowOverlap": true
      },
      "turboThreshold": 2000
    }
  }
}

我只是将这个 JSON 放在字符串中作为 cc 变量并写入

DocumentContext doc = JsonPath.parse(cc).set(JsonPath.compile("$.meta"), "nani");
        doc.json();

        System.out.println(doc);

最佳答案

您正在打印 DocumentContext 实例而不是其内容。

尝试:

System.out.println(doc.read([Your JsonPath]));

here了解更多信息。

关于java - 覆盖 json 的现有属性,或者如果 json 中不存在则添加 next,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56152922/

相关文章:

ios - 在 Swift 2 中将 JSON 转换为数组

php - 从 MySql 中检索最新行(基于日期)

kubectl - 如何使用 kubectl -o jsonpath 仅检索 kubectl.kubernetes.io/last-applied-configuration?

java - Mocking 调用服务方法时

javamail java.io.IOException : Invalid keystore format

java - 整数不会通过套接字发送,并由客户端作为字符串接收

javascript - Django AJAX 防止模板之间重新加载元素

java - "does not include"的 JsonPath 语法,还是负匹配?

json - 使用wiremock匹配JsonPath中的数据

java - KeyPairGenerator 不生成随 secret 钥