比较两个JSON字符串是否完全相等
2017-09-13 22:20
627 查看
RT,比较两个JSON字符串是否完全相等,这里使用google贡献的Gson。
一,no POJO,即不另外创建一个简单Java类
[java] view
plain copy
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\"}";
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
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
JsonParser parser = new JsonParser();
JsonObject obj = (JsonObject) parser.parse(str1);
JsonParser parser1 = new JsonParser();
JsonObject obj1 = (JsonObject) parser1.parse(str2);
System.out.println(obj.equals(obj1));
//method 2
[java] view
plain copy
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
Gson gson1 = new GsonBuilder().create();//or new Gson()
JsonElement e1 = gson1.toJsonTree(str1);//or new Gson()
Gson gson2 = new GsonBuilder().create();
JsonElement e2 = gson2.toJsonTree(str2);
System.out.println(e1.equals(e2));
//method 3
[java] view
plain copy
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
JsonElement e3 = new JsonPrimitive(str1);
JsonElement e4 = new JsonPrimitive(str2);
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
<span style="white-space:pre"> </span>Gson gson = new Gson();//new一个Gson对象
//json字符串
String json = "{\"name\":\"guolicheng\",\"id\":123456,\"date\":\"2013-4-13 12:36:54\"}";
//new 一个Product对象
Product product = new Product();
//将一个json字符串转换为java对象
<strong><span style="color:#ff0000;">product = gson.fromJson(json, Product.class);</span></strong>
//输出
System.out.println("Name:" + product.getName());
System.out.println("Id:" + product.getId());
System.out.println("Date:" + product.getDate());
reference:
使用Gson解析json
最后,提供一份可直接访问(不需要梯子)的Online Gson Doc:http://tool.oschina.net/apidocs/apidoc?api=gson2.2.2。
一,no POJO,即不另外创建一个简单Java类
[java] view
plain copy
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\"}";
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
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
JsonParser parser = new JsonParser();
JsonObject obj = (JsonObject) parser.parse(str1);
JsonParser parser1 = new JsonParser();
JsonObject obj1 = (JsonObject) parser1.parse(str2);
System.out.println(obj.equals(obj1));
//method 2
[java] view
plain copy
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
Gson gson1 = new GsonBuilder().create();//or new Gson()
JsonElement e1 = gson1.toJsonTree(str1);//or new Gson()
Gson gson2 = new GsonBuilder().create();
JsonElement e2 = gson2.toJsonTree(str2);
System.out.println(e1.equals(e2));
//method 3
[java] view
plain copy
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
JsonElement e3 = new JsonPrimitive(str1);
JsonElement e4 = new JsonPrimitive(str2);
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
<span style="white-space:pre"> </span>Gson gson = new Gson();//new一个Gson对象
//json字符串
String json = "{\"name\":\"guolicheng\",\"id\":123456,\"date\":\"2013-4-13 12:36:54\"}";
//new 一个Product对象
Product product = new Product();
//将一个json字符串转换为java对象
<strong><span style="color:#ff0000;">product = gson.fromJson(json, Product.class);</span></strong>
//输出
System.out.println("Name:" + product.getName());
System.out.println("Id:" + product.getId());
System.out.println("Date:" + product.getDate());
reference:
使用Gson解析json
最后,提供一份可直接访问(不需要梯子)的Online Gson Doc:http://tool.oschina.net/apidocs/apidoc?api=gson2.2.2。
相关文章推荐
- Gson:比较两个JSON字符串是否完全相等
- 怎么比较两个json字符串完全相等
- 比较两个字符串是否相等
- 比较两个字符串是否相等,相等输出MATCH,否则NOMATCH
- shell比较两个字符串是否相等
- ORACLE中两个有可能都为NULL的字符串是否相等的比较
- c\c++中比较两个字符串或string对象是否相等
- Java中比较两个字符串是否相等的问题
- Java中比较两个字符串是否相等的问题
- 如何快速比较这两个字符串是否相等?
- 比较两个字符串是否相等的shell代码
- 比较任意两个JSON串是否相等(比较对象是否相等)JAVA版
- c\c++中比较两个字符串或string对象是否相等
- 利用随机化的思想比较两个字符串是否相等
- c\c++中比较两个字符串或string对象是否相等
- c语言比较两个字符串是否相等strcmp
- c\c++中比较两个字符串或string对象是否相等
- 汇编语言: 试编写一段程序,要求比较两个字符串 string1 和 string2 所含字符是否相等,如相 等则显示“MATCH”, 若不相同则显示“NO MATCH”。
- shell比较两个字符串是否相等
- 比较两个字符串是否相等,相等输出MATCH,否则NOMATCH