【问题标题】:how to compare array from sqlite with contents with json array如何将sqlite中的数组与json数组的内容进行比较
【发布时间】:2014-05-08 09:48:18
【问题描述】:

嗨,我在比较来自 sqlite 数据库的数组和来自 web 服务响应的 json 数组的最佳策略时遇到了问题。请告诉我比较这两个数组的最佳方法。这是我的代码:

public void getAllElements() {
     Map<String, String> map = new HashMap<String, String>();
     try {
        //select all query
        db.open();
        Cursor cc= db.getAllEntries();
        try {
            // looping through all rows and adding to list
            if (cc.moveToFirst()) {
                do {
                   map.put("ID",cc.getString(cc.getColumnIndex(DbHelper.ENTRY_ID)));
                } while (cc.moveToNext());
            }
        } finally {
            try { cc.close(); } catch (Exception ignore) {}
        }
    } finally {
         try { db.close(); } catch (Exception ignore) {}
    }
    // return  list 
}

protected String doInBackground(String... params) {
    // Creating service handler class instance

    JSONParser jParser = new JSONParser();
    DefaultHttpClient client = new DefaultHttpClient(); // Thread
    String uri = "url";

    HttpGet httpget = new HttpGet(uri);
    httpget.setHeader("Cookie", cookie);

    try {
        response = client.execute(httpget);
        // response
        String res = EntityUtils.toString(response.getEntity());

        // jarray with the json responde
        jArr = new JSONArray(res);
        System.out.println("response" + jArr);

        for (int i = 0; i < jArr.length(); i++) {
            // create json object
            JSONObject jsonObject = jArr.getJSONObject(i);
            // easy parse fields
            String vid = jsonObject.getString("vid");
            String nid = jsonObject.getString("nid");
            // uid value
            String field_text = jsonObject.getString("field_text");
            String field_edit_ts = jsonObject.getString("field_editts");
            String field_deleted_flag = jsonObject.getString("field_deleted");
            String field_device_name = jsonObject.getString("field_devicename");
            String field_creation_ts = jsonObject.getString("field_creationts");
            String field_device_key = jsonObject.getString("field_devicekey");
         }
    } catch(Exception e){
    }
}

【问题讨论】:

    标签: java android arrays sqlite


    【解决方案1】:

    我建议将您的数据库和 JSON 数据包装在 class 中并实现 equals(Object) 方法。

    对于您的 JSON 数据,一旦您有了一个类(如上所述),我将使用 GSON (https://code.google.com/p/google-gson/) 直接转换为对象

    然后为了您的比较,将您的数据库或 JSON 数据转换为 List(来自 CollectionsHashTable/HashMap(搜索更快))并使用 Collections.contains(Object)(在List) 方法和包含计数,您可以将其与列表的大小进行比较。下面是一个例子,请填空:

    List<YourObject> dbDataList = new ArrayList<YourObject>(Arrays.asList(yourDatabaseDataArray));
    int containsCount = 0;
    for (YourObject o : yourJsonDataArray){
        if (dbDataList.contains(o))
            containsCount ++;
    }
    // Success condition is up to you, I'm just comparing the sizes
    if (containsCount == dbDataList.size()){
        // Success!
    } else {
        // Failure
    }
    

    【讨论】:

      【解决方案2】:
      Map<String, String> map = new HashMap<String, String>();
      

      您正在创建包含两个字符串变量(键和值)的 HasMap。例如,可以使用键搜索值,但不能对 HashMap 进行排序。 (Array)List 可以排序,正如 Artyom 所说。

      map.put("ID",cc.getString(cc.getColumnIndex(DbHelper.ENTRY_ID)));
      

      您将字符串“ID”放入地图每个值的键中。将其更改为 List 并使用 Collections.sort(list,Comparator) 对其进行排序。

      【讨论】:

        【解决方案3】:

        您可以创建类来保存您获得的数据状态并覆盖方法 equals 以在您的类中进行比较。

        此外,为了保存数据,请使用 ArrayList。 用于比较使用的下一个代码。

        Collection<YourObject> one= new ArrayList(Arrays.asList(obj1, obj2, obj3));
        Collection<YourObject> two = new ArrayList(Arrays.asList(obj1, obj2, obj3));
        
        one.equals(two)
        

        【讨论】:

        • 你能给我一些例子吗?谢谢
        猜你喜欢
        • 1970-01-01
        • 2017-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-15
        • 1970-01-01
        • 1970-01-01
        • 2016-04-11
        相关资源
        最近更新 更多