比较两个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 ...
随机推荐
- C++ 初始化函数的实现
http://www.cppblog.com/xlshcn/archive/2007/11/21/37088.aspx
- pandas将字段中的字符类型转化为时间类型,并设置为索引
假设目前已经引入了 pandas,同时也拥有 pandas 的 DataFrame 类型数据. import pandas as pd 数据集如下 df.head(3) date open close ...
- 170619、springboot编程之HelloWorld
springboot资料看了一段时间了,个人觉得开发效率相当高,也参考了网上很多大牛的技术博客,在这里面我也记录一下,方便以后自己翻阅查看,同时也给新手最一点点指引.如果有侵权大牛博客文章,请告诉我, ...
- 报警告session_regenerate_id(): Failed to create(read) session ID: files (path: N;/path)
php.ini文件中的session.save_path = "N;/path"注释掉(前面加分号)
- js判断浏览器是否安装Flash插件,并提示安装或开启
var flashChecker = function() { var hasFlash = 0; //是否安装了flash var flashVersion = 0; //flash版本 if(do ...
- 深究AngularJS——自定义服务详解(factory、service、provider)
前言 3种创建自定义服务的方式. Factory Service Provider 大家应该知道,AngularJS是后台人员在工作之余发明的,他主要应用了后台早就存在的分层思想.所以我们得了解下分 ...
- Qt::浅谈信号槽连接,参数在多线程中的使用
Qt的信号槽有五种连接方式定义在enum Qt::ConnectionType,下面简单介绍 Qt::AutoConnection:自动判断连接方式,如果信号发送对象和执行槽对象在同一线程,那么等于Q ...
- django的request对象和response对象
概述Django 使用 request 和 response 对象表示系统状态数据..当请求一个页面时,Django创建一个 HttpRequest 对象.该对象包含 request 的元数据. 然后 ...
- 详解javascript实现自定义事件
这篇文章主要为大家介绍了javascript实现自定义事件的方法,自定义事件,顾名思义,就是自己定义事件类型,自己定义事件处理函数,javascript如何实现自定义事件,需要了解的朋友可以参考下 我 ...
- React 函数传参
import React, { Component } from 'react'; import { render } from 'react-dom'; class GroceryList exte ...