如何从 json 响应中获取数据作为变量

How to get data from a json response as a variable

我尝试使用 JSONPath 获取名为版本(第一个)的变量的值,但显然我的解决方案根本不起作用。 我尝试使用 $..version 或 $.container..version 之类的表达式。

我的回复如下:

{
  "container" : {
    "version" : 8,
    "updatedBy" : "user111",
    "updatedOn" : "2017-08-17T16:00:24Z",
    "id" : 16,
    "dataEnt" : {
      "dataEntid" : "dataEntid-000032",
      "dataEnttype" : "21"
    },
    "impact" : [ ],
    "operationalFocus" : false,
    "periodicity" : {
      "version" : 0,
      "updatedBy" : "unknown",
      "updatedOn" : "2017-03-31T16:44:08Z",
      "step" : 1,
      "period" : 31084132,
      "_VALIDATION" : {
        "valid" : true,
        "saveAll" : true,
        "reasons" : [ ],
        "details" : {
          "period" : {
            "valid" : true,
            "saveAll" : true,
            "risks" : [ ],
            "rmiCode" : null,
            "rmiMessage" : null
          },
          "version" : {
            "valid" : true,
            "saveAll" : true,
            "risks" : [ ],
            "rmiCode" : null,
            "rmiMessage" : null
          },
          "step" : {
            "valid" : true,
            "saveAll" : true,
            "risks" : [ ],
            "rmiCode" : null,
            "rmiMessage" : null
          }
        },
        "rmiCode" : null,
        "rmiMessage" : null
      },
      "_META" : { }
    }

首先,您粘贴的 JSON 无效:末尾缺少 2 个大括号(根对象和 container 对象未闭合)。如果这不是 SO 上的 copy/paste 错误,而是实际数据问题,您可能需要先更正它。

如果我没理解错的话,您需要变量中此字段的值:

"version" : 8

如果是这样,JSON路径应该是:

$.container.version

container.version

如果您更喜欢相对路径而不是绝对路径。

$..version$.container..version 这样的路径将 select 多个版本字段("version" : 0 in periodicity 属性,并且那个是_VALIDATION)

中的对象

以下表达式将为您提供所需的结果。 变量:ContainerVersion JSON 表达式:$..container.version 现在可以使用以下方式调用存储的版本值:${ContainerVersion}

如果有多个 "version" 标签,那么您可以通过以下表达式加载 "version" 的所有值, $..container.version[*] 您可以将变量称为 ${Var_1}${Var_2} 等。 添加调试采样器以查看加载的变量名称及其对应的值。

希望以上内容对您有所帮助...