Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 26th, 2012  |  syntax: None  |  size: 5.71 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I flag duplicates in an array?
  2. [{"lon": 0.001, "lat": 0.001, "desc": test}, {"lon": 0.001, "lat": 0.001, "desc": test2}]
  3.        
  4. [{"lon": 0.001, "lat": 0.001, "desc": test, test2}]
  5.        
  6. //Store locPoints from server in JSONArray
  7. JSONArray jPointsArray = new JSONArray(serverData);
  8. List<JSONObject> jObjects = new ArrayList<JSONObject>();
  9. List<JSONObject> seenObjects = new ArrayList<JSONObject>();
  10.  
  11.  for(int i = 0; i < jPointsArray.length(); ++i)
  12. {
  13.  jObjects.add(jPointsArray.getJSONObject(i));
  14. }        
  15.  for (JSONObject obj : jObjects)
  16.                 {
  17.                     //This always returns true
  18.                     if (!seenObjects.contains(obj))// && !seenObjects.contains(obj.get("lon")))
  19.                     {
  20.  
  21.                         Log.i("Sucess", "Huzzah!");
  22.                         seenObjects.add(obj);
  23.                     }
  24.                     else
  25.                     {
  26.                          //merge the 'desc' field in 'obj' with the 'desc' field in
  27.                          JSONObject original = (JSONObject)seenObjects.get(seenObjects.indexOf(obj));
  28.                          JSONObject update = obj;
  29.                          original.put("desc", original.get("desc") + ", " + update.get("desc"));
  30.                          seenObjects.get(seenObjects.indexOf(obj)).get("desc"));
  31.  
  32.                     }
  33.                 }
  34.        
  35. //assuming that the array you are filtering is called 'myArray'
  36. List<Object> seenObjects = new ArrayList<Object>();
  37. for (Object obj : myArray) {
  38.     if (! seenObjects.contains(obj)) {
  39.         seenObjects.add(obj);
  40.     }
  41.     else {
  42.         //merge the 'desc' field in 'obj' with the 'desc' field in
  43.         //'seenObjects.get(seenObjects.indexOf(obj))'
  44.     }
  45. }
  46.        
  47. import java.util.ArrayList;
  48. import java.util.List;
  49.  
  50. import org.json.simple.JSONObject;
  51. import org.json.simple.JSONValue;
  52.  
  53. public class JsonMergeTest {
  54.     @SuppressWarnings({ "rawtypes", "unchecked" })
  55.     public static void main(String[] args) {
  56.         List<Object> myArray = new ArrayList<Object>();
  57.         myArray.add(MyJsonObject.parse("{"lon": 0.001, "lat": 0.001, "desc": "test"}"));
  58.         myArray.add(MyJsonObject.parse("{"lon": 0.001, "lat": 0.001, "desc": "test2"}"));
  59.  
  60.         List seenObjects = new ArrayList<Object>();
  61.         for (Object obj : myArray) {
  62.             if (! seenObjects.contains(obj)) {
  63.                 seenObjects.add(obj);
  64.             }
  65.             else {
  66.                 //merge the 'desc' field in 'obj' with the 'desc' field in the list
  67.                 MyJsonObject original = (MyJsonObject)seenObjects.get(seenObjects.indexOf(obj));
  68.                 MyJsonObject update = (MyJsonObject)obj;
  69.                 original.put("desc", original.get("desc") + ", " + update.get("desc"));
  70.             }
  71.         }
  72.  
  73.         for (MyJsonObject obj : (List<MyJsonObject>)seenObjects) {
  74.             System.out.println(obj.toJSONString());
  75.         }
  76.     }
  77.  
  78.     private static class MyJsonObject extends JSONObject  {
  79.         @Override
  80.         public boolean equals(Object obj) {
  81.             if (obj == null || ! (obj instanceof MyJsonObject) || ! this.containsKey("lat") || ! this.containsKey("lon")) {
  82.                 return super.equals(obj);
  83.             }
  84.             MyJsonObject jsonObj = (MyJsonObject)obj;
  85.             return this.get("lat").equals(jsonObj.get("lat")) && this.get("lon").equals(jsonObj.get("lon"));
  86.         }
  87.  
  88.         @Override
  89.         public int hashCode() {
  90.             if (! this.containsKey("lat") || ! this.containsKey("lon")) {
  91.                 return super.hashCode();
  92.             }
  93.             return this.get("lat").hashCode() ^ this.get("lon").hashCode();
  94.         }
  95.  
  96.         @SuppressWarnings("unchecked")
  97.         public static Object parse(String json) {
  98.             Object parsedJson = JSONValue.parse(json);
  99.             if (! (parsedJson instanceof JSONObject)) {
  100.                 return parsedJson;
  101.             }
  102.  
  103.             MyJsonObject result = new MyJsonObject();
  104.             result.putAll((JSONObject)parsedJson);
  105.             return result;
  106.         }
  107.     }
  108. }
  109.        
  110. public class Location implements Comparable<Location> {
  111.     public String lon;
  112.     public String lat;
  113.     public String desc;
  114.  
  115.     @Override
  116.     public String toString() {
  117.         return "<lon: " + lon +", lat: "+ lat +", desc: " + desc +">";
  118.     }
  119.  
  120.     @Override
  121.     public boolean equals(Object obj) {
  122.         return ((Location)obj).lon.equals(lon) && ((Location)obj).lat.equals(lat);
  123.     }
  124.  
  125.     public int compareTo(Location obj) {
  126.         return ((Location)obj).lon.compareTo(lon) + ((Location)obj).lat.compareTo(lat);
  127.     }
  128.  
  129.  
  130. }
  131.        
  132. public static void main(String[] args){
  133.       //Some test data
  134.     String s = "[" +
  135.             " {"lon": 0.001, "lat": 0.001, "desc": "test"}," +
  136.             " {"lon": 0.002, "lat": 0.001, "desc": "test3"}," +
  137.             " {"lon": 0.002, "lat": 0.005, "desc": "test4"}," +
  138.             " {"lon": 0.002, "lat": 0.001, "desc": "test5"}," +
  139.             " {"lon": 0.001, "lat": 0.001, "desc": "test2"}]";
  140.     Gson gson = new Gson();
  141.     Location[] al = gson.fromJson(s, Location[].class);
  142.     List<Location> tl = Arrays.asList(al);
  143.  
  144.      //lets sort so that similar locations are grouped
  145.     Collections.sort(tl);
  146.     List<Location> fl = new ArrayList<Location>();
  147.     Location current = null;
  148.  
  149.      //merge!
  150.     for(Iterator<Location> it = tl.iterator(); it.hasNext();){
  151.         current = current==null?it.next():current;
  152.         Location ltmp = null;
  153.         while(it.hasNext() && (ltmp = it.next()).equals(current))
  154.             current.desc = current.desc + "," + ltmp.desc;
  155.         fl.add(current);
  156.         current = ltmp;
  157.     }
  158.  
  159.        //convert back to JSON?
  160.     System.out.println(gson.toJson(fl));
  161.  
  162. }
  163.        
  164. [{"lon":"0.002","lat":"0.005","desc":"test4"},
  165. {"lon":"0.002","lat":"0.001","desc":"test3,test5"},
  166. {"lon":"0.001","lat":"0.001","desc":"test,test2"}]