比较两个JSON字符串是否完全相等

RT,比较两个JSON字符串是否完全相等,这里使用google贡献的Gson。


一,no POJO,即不另外创建一个简单Java类

[java] view plain copy
  1. String str1 = "{\"properties\":{\"packet\":{\"recorded_at\":\"2015-09-02 04:45:45 +0000\",\"userId\":\"100000000000001\",\"meta\":{\"account\":\"xxx\",\"event\":\"track\"},\"fields\":{\"gyroData\":{\"rotation_y\":-1,\"rotation_z\":-1,\"rotation_x\":-1},\"accelerometerData\":{\"acceleration_x\":-1,\"acceleration_z\":-1,\"acceleration_y\":-1},\"location\":{\"speed\":4.68,\"speed_course\":0.7,\"horizontal_accuracy\":10,\"longtitude\":-122.02359082,\"vertical_accuracy\":-1,\"latitude\":37.33385024},\"pedometerData\":{\"step_count\":0}},\"recorded_sample_rate\":5}},\"geometry\":{\"type\":\"Point\",\"coordinates\":[37.33385024,-122.02359082]},\"type\":\"Feature\"}";  
  2. String str2 = "{\"properties\":{\"packet\":{\"recorded_at\":\"2015-09-02 04:45:45 +0000\",\"userId\":\"100000000000001\",\"meta\":{\"account\":\"xxx\",\"event\":\"track\"},\"fields\":{\"gyroData\":{\"rotation_y\":-1,\"rotation_z\":-1,\"rotation_x\":-1},\"accelerometerData\":{\"acceleration_x\":-1,\"acceleration_z\":-1,\"acceleration_y\":-1},\"location\":{\"speed\":4.68,\"speed_course\":0.7,\"horizontal_accuracy\":10,\"longtitude\":-122.02359082,\"vertical_accuracy\":-1,\"latitude\":37.33385024},\"pedometerData\":{\"step_count\":0}},\"recorded_sample_rate\":5}},\"geometry\":{\"type\":\"Point\",\"coordinates\":[37.33385024,-122.02359082]},\"type\":\"Feature\"}";  
  3.           


// method 1
[java] view plain copy
  1. import com.google.gson.JsonObject;  
  2. import com.google.gson.JsonParser;  
  3.   
  4.   
  5.         JsonParser parser = new JsonParser();  
  6.         JsonObject obj = (JsonObject) parser.parse(str1);  
  7.         JsonParser parser1 = new JsonParser();  
  8.         JsonObject obj1 = (JsonObject) parser1.parse(str2);  
  9.           
  10.         System.out.println(obj.equals(obj1));  

//method 2

[java] view plain copy
  1. import com.google.gson.Gson;  
  2. import com.google.gson.GsonBuilder;  
  3. import com.google.gson.JsonElement;  
  4.   
  5. Gson gson1 = new GsonBuilder().create();//or new Gson()   
  6.         JsonElement e1 = gson1.toJsonTree(str1);//or new Gson()   
  7.           
  8.         Gson gson2 = new GsonBuilder().create();  
  9.         JsonElement e2 = gson2.toJsonTree(str2);  
  10.         System.out.println(e1.equals(e2));  

//method 3

[java] view plain copy
  1. import com.google.gson.JsonElement;  
  2. import com.google.gson.JsonPrimitive;  
  3.   
  4. JsonElement e3 = new JsonPrimitive(str1);  
  5.         JsonElement e4 = new JsonPrimitive(str2);  
  6.         System.out.println(e3.equals(e4));  


reference:

Gson: Directly convert String to JsonObject (no POJO)


二,使用简单POJO类,和mentor Yang讨论过这个问题,哪怕这个JSON字符串有多么复杂,一般情况下五层就达到上限了(上面那个Json String看起来那么”复杂“,才三层)。

这里只是举个简单的栗子。因为这种方法看起来比第一种方式麻烦多了。

步骤就是先建一个(或者多个)POJO类,类中的属性名和JSON字符串中的key名一一对应。

然后:

[java] view plain copy
  1. <span style="white-space:pre">        </span>Gson gson = new Gson();//new一个Gson对象  
  2.         //json字符串  
  3.         String json = "{\"name\":\"guolicheng\",\"id\":123456,\"date\":\"2013-4-13 12:36:54\"}";  
  4.         //new 一个Product对象  
  5.         Product product = new Product();  
  6.         //将一个json字符串转换为java对象  
  7.         <strong><span style="color:#ff0000;">product = gson.fromJson(json, Product.class);</span></strong>  
  8.         //输出  
  9.         System.out.println("Name:" + product.getName());  
  10.         System.out.println("Id:" + product.getId());  
  11.         System.out.println("Date:" + product.getDate());  

reference:

使用Gson解析json


最后,提供一份可直接访问(不需要梯子)的Online Gson Doc:http://tool.oschina.net/apidocs/apidoc?api=gson2.2.2。

posted on   小小鸟儿!  阅读(7976)  评论(0编辑  收藏  举报

编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· MQ 如何保证数据一致性?
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
历史上的今天:
2016-12-20 自定义RecyclerView.ItemDecoration,实现RecyclerView的分割线效果
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示