比較兩個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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM