简体   繁体   English

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

[英]How to compare two different JSON response structure in java?

I have to compare two different JSON structure and want to know the mismatch's taken place in the response of each node.The response compared will be having different attribute name but values are equal.I need to know the way to map the attributes of the two response. 我必须比较两个不同的JSON结构,并想知道每个节点的响应中发生的不匹配情况。比较的响应将具有不同的属性名称,但值相等。我需要知道映射这两个属性的方式响应。

example: 例:

I have to provide the overall comparison like(just a template to compare the attributes) 我必须提供整体比较,就像(只是一个模板来比较属性)

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

So,that after comparing my result, it should display the mismatch in the value of the attribute in my log. 因此,在比较我的结果之后,它应该在我的日志中显示属性值的不匹配。

Still now, i am just using the jmeter for getting the response from the call and writing the java code in the beanshell sampler.The package I am using rite now is json.org .Now, i am comparing each attribute and printing the logs.The purpose of doing this is, we get json data from client but the datas which are required are filtered and constructed again using our code.So tat a structured JSON is formed and I have to verify and print the log error. 现在,我仍然只是使用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"
    }
  }
}

First of all I'd recommend using of JSONPathExtractor Post Processor component which provided by JMeter Plugin (you'll need "Extras with Libs set" one). 首先,我建议使用JMeter插件提供的JSONPathExtractor Post Processor组件(您需要“带Libs集的其他功能”)。

See Parsing JSON section on how to use JSON Path. 有关如何使用JSON路径,请参见解析JSON部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM