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\"}";

// method 1

[java] view plain copy
  1. import com.google.gson.JsonObject;
  2. import com.google.gson.JsonParser;
  3. JsonParser parser = new JsonParser();
  4. JsonObject obj = (JsonObject) parser.parse(str1);
  5. JsonParser parser1 = new JsonParser();
  6. JsonObject obj1 = (JsonObject) parser1.parse(str2);
  7. 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. Gson gson1 = new GsonBuilder().create();//or new Gson()
  5. JsonElement e1 = gson1.toJsonTree(str1);//or new Gson()
  6. Gson gson2 = new GsonBuilder().create();
  7. JsonElement e2 = gson2.toJsonTree(str2);
  8. 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. JsonElement e3 = new JsonPrimitive(str1);
  4. JsonElement e4 = new JsonPrimitive(str2);
  5. 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。

最新文章

  1. [AR+Vuforia]学习笔记
  2. openGL漫游功能简单实现
  3. django部署
  4. Redis应用配置项说明
  5. Python之编写函数
  6. C#中的委托和事件(续)
  7. Storm系列(十二)架构分析之Worker-心跳信息处理
  8. ZOJ2834--Maximize Game Time(树形DP)
  9. HDU 1251 字典树(前缀树)
  10. Android自己定义组件系列【2】——Scroller类
  11. shell 创建带参数的命令方法
  12. Android开发——使用高级的RecyclerView实现侧滑菜单删除功能(SwipeRecyclerView)
  13. global与nonlocal关键字总结
  14. LeetCode(116):填充同一层的兄弟节点
  15. Django xadmin引入DjangoUeditor
  16. sublime text 文件打开时回调一些函数
  17. 【RF库XML测试】Get Elements
  18. sql distinct 去除重复的字段
  19. b7
  20. rac下asm管理的表空间-数据文件的重命名

热门文章

  1. 解决在微信中部分IOS不能自动播放背景音乐
  2. nodejs MySQL操作
  3. 【BZOJ4282】慎二的随机数列 乱搞
  4. 有关velocity的资料(等待整理)
  5. c# DataTable、DataSet、DataReader
  6. Oracle性能优化之oracle里表、索引、列的统计信息
  7. Error:(12, 64) java: 未报告的异常错误java.io.IOException; 必须对其进行捕获或声明以便抛出
  8. Network of Schools---poj1236(强连通分量)
  9. Linux常用命令【总结】
  10. Flask系列(九)flask-script组件