【问题标题】:How to compare two different JSON response structure in java?如何在java中比较两种不同的JSON响应结构?
【发布时间】:2014-02-01 07:11:47
【问题描述】:

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

示例:

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

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

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

现在,我只是使用 jmeter 从调用中获取响应并在 beanshell 采样器中编写 java 代码。我现在使用 rite 的包是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"
    }
  }
}

【问题讨论】:

  • 到目前为止你有什么?
  • 我建议您查看库以进行文本或对象比较,但您应该向我们描述您已经尝试过的内容。在我们花时间给出答案之前,我们希望看到尝试解决问题、研究问题等。
  • 提示:这听起来像是统一算法的用例...en.wikipedia.org/wiki/Unification_%28computer_science%29

标签: java arrays json object jmeter


【解决方案1】:

首先,我建议使用JMeter Plugin 提供的JSONPathExtractor 后处理器组件(您需要“带有库集的附加组件”之一)。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多