如何比较在Java中两个不同的JSON响应结构? [英] How to compare two different JSON response structure in java?

查看:548
本文介绍了如何比较在Java中两个不同的JSON响应结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要比较两个不同的JSON结构,想知道相比将具有不同的属性名称,但值equal.I每个node.The响应的响应不匹配的发生需要知道映射属性的方式的两个响应。

例如:

我要像提供整体比较(只是一个模板进行比较的属性)

 信息阵------------>详情阵列
图像属性------------>照片属性
name属性------------>文本属性
type属性------------>使用属性
主页对象------------>首页上对象

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

现在尽管如此,我只是用JMeter的用于获取呼叫的响应和写入我使用仪式现在BeanShell的sampler.The包的java code是的 json.org 。现在,我比较每个属性,并打印这样做是对logs.The目的,我们从客户端获得JSON数据,但它们需要的数据都是过滤,并使用我们的code.So TAT形成结构化的JSON再次构造,我必须确认和打印日志错误。

 假设考虑两个响应结构:JSON答:{
  信息:
    {
      形象:small.jpg
      名:flipkart_small
      类型:标志
    },
    {
      形象:medium.jpg
      名:flipkart_medium
      类型:弹出
    },
    {
      形象:large.jpg
      名:flipkart_large
      类型:旗帜
    }
  ]
  主页:{
    电子:{
      名:电子,
      类型:类别,
      链接:http://flipkart.com/Electronic
    },
    图片: {
      SRC:图像/ samsung_s4.png
      名:S4
      宽:250,
      高度:250,
      对齐方式:中心
    },
    描述:{
      文:Flipkart是在印度网上购物的主要目的地,
      大小:36,
      风格:大胆,
      名:text1中,
      对齐方式:自圆其说
    }
  }
}
JSON乙:{
  详细信息:
    {
      照片:small.jpg
      文:flipkart_small
      用:标志
    },
    {
      照片:medium.jpg
      文:flipkart_medium
      用:弹出
    },
    {
      照片:large.jpg
      文:flipkart_large
      用:旗帜
    }
  ]
  TOPPAGE:{
    ElectronicSection:{
      文:电子,
      用:类别,
      URL:http://flipkart.com/Electronic
    },
    照片:{
      路径:图像/ samsung_s4.png
      名:S4
      W:250,
      H:250,
      显示:中心
    },
    信息:{
      DESP:Flipkart是在印度网上购物的主要目的地,
      长度:36,
      风格:大胆,
      值:text1中,
      显示:自圆其说
    }
  }
}


解决方案

首先我会建议使用的它通过提供JSONPathExtractor 后处理器组件 JMeter的插件(你需要其他与利布斯设置一节)。

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

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.

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.

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).

See Parsing JSON section on how to use JSON Path.

这篇关于如何比较在Java中两个不同的JSON响应结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆