基于特定值提取的Json路径

时间:2013-09-17 05:33:32

标签: scala gatling

我在gatling中有需要提取特定JSON值的场景。例如:我想提取书名“世纪的谚语”的作者。我正在使用以下Jsonpath $..book[?(@.title eq 'Sayings of the Century')]。但它没有给我任何回报。你能告诉我如何实现这个目标。

{ "store": {
    "book": [ 
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

2 个答案:

答案 0 :(得分:1)

正确的语法是==,而不是eq

您可以使用http://ashphy.com/JSONPathOnlineEvaluator

等在线工具测试您的表达方式

答案 1 :(得分:0)

java和scala不同。您应该在scala中清楚显示jsonPath的类型。

例如,在Java中,代码为:

JsonPath.read(jsonString, "$.message_metadata.event_type");

但是在scala中是:

JsonPath.read\[String\](jsonString, "$.message_metadata.event_type")

如果您的结果类型为JSONArray,则代码如下:

JsonPath.read\[JSONArray\](jsonString, "$.message_metadata.event_type")

希望这可以为您提供帮助。