如何在java中比较两种不同的JSON响应结构?

时间:2014-02-01 07:11:48

标签: java arrays json object jmeter

我必须比较两个不同的JSON结构,并想知道每个节点的响应中发生的不匹配。比较的响应将具有不同的属性名称但值是相等的。我需要知道映射属性的方式两个回应。

示例:

我必须提供整体比较,例如(只是比较属性的模板)

info array          ------------> Details array
image attribute     ------------> photo attribute     
name attribute      ------------> text attribute     
type attribute      ------------> use attribute
homepage object     ------------> Toppage object

因此,在比较我的结果后,它应该在我的日志中显示属性值的不匹配。

现在,我只是使用jmeter来获取调用的响应并在beanshell sampler中编写java代码。我现在使用的是json.org。现在,我正在比较每个属性这样做的目的是,我们从客户端获取json数据,但是使用我们的代码对所需的数据进行过滤和再次构建。结果形成了一个结构化的JSON,我必须验证并打印日志错误

Suppose Consider Two response Structure:

JSON A:

{
  "info": [
    {
      "image": "small.jpg",
      "name": "flipkart_small",
      "Type": "logo"
    },
    {
      "image": "medium.jpg",
      "name": "flipkart_medium",
      "Type": "popup"
    },
    {
      "image": "large.jpg",
      "name": "flipkart_large",
      "Type": "banner"
    }
  ],
  "homepage": {
    "Electronic": {
      "name": "Electronic",
      "type": "category",
      "link": "http://flipkart.com/Electronic"
    },
    "image": {
      "src": "Images/samsung_s4.png",
      "name": "s4",
      "width": 250,
      "height": 250,
      "alignment": "center"
    },
    "Description": {
      "text": "Flipkart is a leading destination for online shopping in India.",
      "size": 36,
      "style": "bold",
      "name": "text1",
      "alignment": "justify"
    }
  }
}


JSON B:

{
  "Details": [
    {
      "photo": "small.jpg",
      "text": "flipkart_small",
      "use": "logo"
    },
    {
      "photo": "medium.jpg",
      "text": "flipkart_medium",
      "use": "popup"
    },
    {
      "photo": "large.jpg",
      "text": "flipkart_large",
      "use": "banner"
    }
  ],
  "Toppage": {
    "ElectronicSection": {
      "text": "Electronic",
      "use": "category",
      "url": "http://flipkart.com/Electronic"
    },
    "photo": {
      "path": "Images/samsung_s4.png",
      "name": "s4",
      "w": 250,
      "h": 250,
      "display": "center"
    },
    "information": {
      "desp": "Flipkart is a leading destination for online shopping in India.",
      "length": 36,
      "style": "bold",
      "value": "text1",
      "display": "justify"
    }
  }
}

1 个答案:

答案 0 :(得分:1)

首先,我建议使用由JSONPathExtractor提供的JMeter Plugin后处理器组件(您需要“Libs set的附加组件”)。

请参阅Parsing JSON部分,了解如何使用JSON路径。