比较两个JSON字符串是否完全相等
RT,比较两个JSON字符串是否完全相等,这里使用google贡献的Gson。
一,no POJO,即不另外创建一个简单Java类
- 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
- 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
- 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
- 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名一一对应。
然后:
- <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:
最后,提供一份可直接访问(不需要梯子)的Online Gson Doc:http://tool.oschina.net/apidocs/apidoc?api=gson2.2.2。
比较两个JSON字符串是否完全相等的更多相关文章
- 基于开源库jsoncpp的json字符串解析
json(JavaScript Object Notation)是一种轻量级高效数据交换格式.相比于XML,其更加简洁,解析更加方便.在实习期间,我负责的程序模块,多次使用到json进行数据传输.由于 ...
- java解析多层嵌套json字符串
java分别解析下面两个json字符串 package jansonDemo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjso ...
- python字典转化成json格式。JSONEncoder和JSONDecoder两个类来实现Json字符串和dict类型数据的互相转换
遇到问题:进行Webservice接口测试时,对接口入参数据进行了处理,变成了dict格式,去进行接口请求报错. 需要转成成json格式,双引号去扩. 如下: 更改代码: # 在Python标准库的j ...
- Gson解析复杂JSON字符串的两种方式
JSON解析可以使用的库: JSONObject(源自Android官方). Gson(源自Google). Jackson(第三方开源库). FastJSON(第三方开源库). 本文例子使用Goog ...
- spring接收json字符串的两种方式
一.前言 前几天遇到一个问题,前端H5调用我的springboot一个接口(post方式,@RequestParameter接收参数),传入的参数接收不到.自己测试接口时使用postman的form- ...
- @ResponseBody 返回json字符串的核心类是org.springframework.http.converter.json.MappingJacksonHttpMessageConverter,它使用了Jackson 这个开源的第三方类库。主要是以下两个jar包:jackson-core-asl-1.6.4.jar;jackson-mapper-asl-1.6.4.jar
@ResponseBody 返回json字符串的核心类是org.springframework.http.converter.json.MappingJacksonHttpMessageConvert ...
- java后台处理解析json字符串的两种方式
简单说一下背景 上次后端通过模拟http请求百度地图接口,得到的是一个json字符串,而我只需要其中的某个key对应的value. 当时我是通过截取字符串取的,后来觉得不太合理,今天整理出了两种处理解 ...
- Redis:存储对象的两种方式(序列化和json字符串)
方式一:序列化操作 public class SerializeUtil { /* * 序列化 * */ public static byte[] serizlize(Object ...
- Java - 格式化输出JSON字符串的两种方式
目录 1 使用阿里的fastjson 1.1 项目的pom.xml依赖 1.2 Java示例代码 2 使用谷歌的gson 2.1 项目的pom.xml依赖 2.2 Java示例代码 1 使用阿里的fa ...
随机推荐
- poj3734 Blocks[矩阵优化dp or 组合数学]
Blocks Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6578 Accepted: 3171 Descriptio ...
- MySQL命令行基本命令操作
进入命令模式后, 显示所有数据库 show databases; 选定某个数据库 use 数据库名; 创建数据库 create database 数据库名; 删除数据库 drop table 数据库名 ...
- postgresql----SELECT
示例1.简单查询 使用*查询表所有的字段,也可以指定字段名查询 test=# select * from tbl_insert; a | b ---+---- | sd | ff ( rows) te ...
- 沈阳网络赛G-Spare Tire【容斥】
17.64% 1000ms 131072K A sequence of integer \lbrace a_n \rbrace{an} can be expressed as: \display ...
- java 常见几种发送http请求案例
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java ...
- HanLP https://pypi.python.org/pypi/sumy/
HanLP - 汉语言处理包 http://hanlp.linrunsoft.com/doc.html https://pypi.python.org/pypi/sumy/
- proxy ubuntu proxy--http://jingyan.baidu.com/article/8cdccae9913470315513cd70.html
apt-get 设置代理 proxy 方法 方法一 :这是一种临时的手段,如果你仅仅是暂时需要通过http代理使用apt-get,你可以使用这种方法. 在使用 apt-get 之前,在终端中输入以下 ...
- TA-Lib函数对照
Overlap Studies 重叠研究指标 BBANDS Bollinger Bands 布林带 DEMA Double Exponential Moving Average 双指数移动平均线 EM ...
- importlib应用 - django
背景 仿django的中间件的编程思想 用户可通过配置,选择是否启用某个组件/某个功能,只需要配置 eg:报警系统,发邮件,发微信 ... ( 根据字符串导入模块, 利用反射找到模块下的类,实例化.执 ...
- Navigation Nightmare---poj1984(多关系并查集)
题目链接:http://poj.org/problem?id=1984 给定n个城市,m条边告诉你城市间的相对距离,接下来q组询问,问你在第几条边添加后两城市的距离. #include <ios ...