比较2个JSONObjects

问题描述 投票:1回答:3

我的问题是这样的:https://stackoverflow.com/questions/20919343/php-getting-count-of-how-many-x-and-y-in-a-row-then-getting-other-data-of-x-and

但后来我考虑从Android部分完成这项工作。所以我有两个从php生成的JSONObjects。第一个对象具有“值”和“总”数组。第二个有“名字”,“姓”和“数字”以及更多的数组。现在我需要比较第二个JSONObject的数字和第一个的值。我尝试了一些循环,但它按预期创建了两倍。这是我的onPostExecute方法:

protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
         pDialog.dismiss();
         try {
             JSONObject jsonObj = new JSONObject(jsonStr);
             JSONObject jsonObj2 = new JSONObject(jsonStr);

             // Getting JSON Array node
             sonuc = jsonObj.getJSONArray(TAG_BASLIK);

             // looping through All Contacts
             for (int i = 0; i < sonuc.length(); i++) {
                 JSONObject c = sonuc.getJSONObject(i);

                  AdayNumara = c.getString(TAG_OY);
                  ToplamOy = c.getString(TAG_TOPLAM);

                 Log.i("Aday Numaraları", AdayNumara);
                 Log.i("Oy Sayısı", ToplamOy);



                 pie.addItem(AdayNumara,Float.valueOf(ToplamOy), pRenk.get(i));

                 adaylar = jsonObj2.getJSONArray(TAG_ADAYLAR);
                for(int j=0; j< adaylar.length(); j++){

                    JSONObject c2 = adaylar.getJSONObject(j);

                    String no = c2.getString(TAG_NO);
                    String ad = c2.getString(TAG_AD);
                    String soyad = c2.getString(TAG_SOYAD);
                    String resim = c2.getString(TAG_RESIM);



                    HashMap<String, String> aday = new HashMap<String, String>();

                    do{
                    aday.put(TAG_AD, ad);
                    aday.put(TAG_SOYAD, soyad);
                    aday.put(TAG_RESIM, resim);
                    aday.put(TAG_NO, no);
                    aday.put(TAG_TOPLAM, ToplamOy);

                    adayList.add(aday);} while(AdayNumara == no);
                    }

                     adapter=new LazyAdapter2(SecimSonuclari.this, adayList);        
                     lv.setAdapter(adapter);
              }



          }catch (JSONException e) {
             e.printStackTrace();
         }


         }

由于我是Android的新手,因此代码可能有错误的方法。

我现在得到的是listview中的价值。必须有2.前两个在数字行上具有相同的值,如1和1。第二对夫妇有另一个号码,比方说2。

android jsonobject
3个回答
1
投票

我知道这个问题在一年之内,但无论如何......我需要比较两个JSONObject值没有内部JSONObject / JSONArray。大多数SO解决方案都推荐使用第三方java库,这有点太重了。

首先,Android的内置JSONObject没有以任何合理的方式定义hashCode()和equals()。我使用了一个内置JSONObject的包装类。

下面的函数放在一些实用程序类中,使用它们来定义hashCode()和equals()是一项简单的练习。

public static int jsonHashCode(JSONObject j) {
    if (j == null) {
        return 0;
    }
    int res = 0;
    for (String s : new AsIterable<String>(j.keys())) {
        res += s.hashCode();
    }
    return res;
}
public static boolean comparesEqual(JSONObject x, JSONObject y, Collection<String>only, Collection<String>except) {
    Set<String> keys = keySet(x);
    keys.addAll(keySet(y));
    if (only != null) {
        keys.retainAll(only);
    }
    if (except != null) {
        keys.removeAll(except);
    }
    for (String s : keys) {
        Object a = null, b = null;
        try {
            a = x.get(s);
        } catch (JSONException e) {
        }
        try {
            b = x.get(s);
        } catch (JSONException e) {
        }
        if (a != null) {
            if (!a.equals(b)) {
                return false;
            }
        } else if (b != null) {
            if (!b.equals(a)) {
                return false;
            }
        }
    }
    return true;
}
public static Set<String> keySet(JSONObject j) {
    Set<String> res = new TreeSet<String>();
    for (String s : new AsIterable<String>(j.keys())) {
        res.add(s);
    }
    return res;
}

其中AsIterable允许我们使用带有迭代器的for(:)循环,并定义为:

public class AsIterable<T> implements Iterable<T> {
    private Iterator<T> iterator;
    public AsIterable(Iterator<T> iterator) {
        this.iterator = iterator;
    }
    public Iterator<T> iterator() {
        return iterator;
    }
}

欢迎您编写并发布此代码的递归版本,支持JSONObject / JSONArray值。


0
投票

我发现了我的问题。似乎使用“==”是错误的。

if(AdayNumara.equals(no)){
                        aday.put(TAG_AD, ad);
                        aday.put(TAG_SOYAD, soyad);
                        aday.put(TAG_RESIM, resim);
                        aday.put(TAG_NO, no);
                        aday.put(TAG_TOPLAM, ToplamOy);

                        adayList.add(aday);

                    }

0
投票

对于我的用例,我最终迭代了第一个json对象的键并检查该值是否与第二个json对象中的值匹配。

            JSONObject obj = new JSONObject("some json string");
            JSONObject comparison = new JSONObject("some other json string with the same data but different key order");

            boolean equal = true;
            for (Iterator<String> iterator = obj.keys(); iterator.hasNext(); ) {
                String curKey = iterator.next();
                if(!obj.getString(curKey).equals(comparison.getString(curKey))){
                    equal = false;
                    break;
                }
            }
            if(equal){
                // do the thing that i want to do if the two objs are equal
            }
© www.soinside.com 2019 - 2024. All rights reserved.