JMeter JSON解析 [英] JMeter JSON parse
问题描述
当响应数据以多余的字符开头时,需要帮助解析JSON.
Need help parsing JSON when the response data starts with extraneous chars.
我能够使用JSON提取查询从示例中提取数据-$.results
I am able to extract data from the example using JSON extraction query - $.results
**Working example:**
{
"results" : [
{
"address_components" : [
{
"long_name" : "2300",
"short_name" : "2300",
"types" : [ "street_number" ]
},
{
"long_name" : "22201",
"short_name" : "22201",
"types" : [ "postal_code" ]
}
]
}
],
"status" : "OK"
}
但是,我无法找到任何合适的方法来从以下JSON对象提取数据:
However, I am unable to find any suitable methods to extract data from the following JSON object:
/**/_xdc_._9l6mlb && _xdc_._9l6mlb( {
"results" : [
{
"address_components" : [
{
"long_name" : "2300",
"short_name" : "2300",
"types" : [ "street_number" ]
},
{
"long_name" : "22201",
"short_name" : "22201",
"types" : [ "postal_code" ]
}
]
}
],
"status" : "OK"
}
)
尝试使用通配符--*$.results
-但没有运气
Tried using wildcard to escape the leading chars before JSON object with - *$.results
- but no luck
任何帮助将不胜感激
推荐答案
此/**/_xdc_._9l6mlb && _xdc_._9l6mlb(
位使您的JSON有点无效.我建议使用 JSR223 PostProcessor 和
This /**/_xdc_._9l6mlb && _xdc_._9l6mlb(
bit makes your JSON a little bit invalid. I would suggest using JSR223 PostProcessor and Groovy language instead like:
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def response = prev.getResponseDataAsString()
def cleanResponse = response.substring(response.indexOf("{"), response.lastIndexOf(")"))
def jsonSlurper = new JsonSlurper()
def json = jsonSlurper.parseText(cleanResponse)
def results = json.results
def builder = new JsonBuilder(results)
vars.put("results", builder.toPrettyString())
上面的代码将与JSON Path PostProcessor完全相同,并将值存储到${results}
JMeter变量
The above code will do absolutely the same as JSON Path PostProcessor and store the value into ${results}
JMeter Variable
参考文献:
- Parsing and producing JSON
- Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For!
这篇关于JMeter JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!