试图在android中合并两个JSONobjects

时间:2014-07-17 14:54:34

标签: java android json eclipse

我正在尝试使用以下代码合并android中的两个json对象:

Iterator<?> i1 = store_ordinary_data.keys();
Iterator<?> i2 = store_premium_data.keys();
while(i1.hasNext()) {
    tmp_key = (String) i1.next();
    mergedObj.put(tmp_key, store_ordinary_data.get(tmp_key));
}
while(i2.hasNext()) {
    tmp_key = (String) i2.next();
    mergedObj.put(tmp_key, store_premium_data.get(tmp_key));

生成的合并JSON应该包含5个数据。但是缺少一个数据。我在上面的代码中做错了什么?

编辑JSON:

{
category: [2]
0:  "Hospital"
1:  "Hotel"
-
store_ordinary_data: {
Hospital: [2]
0:  {
store_name: "KIMS Hospital"
store_image: "index.jpg"
store_address: "near mecdical college"
store_phone: "74126842365"
store_description: "fdsfdsfdff fdsf dsf dsf "
store_url: "dfffsfsdfsfs"
store_latitude: "8.505786"
store_longitude: "76.960194"
store_fb_url: "https://www.facebook.com/iglooappschool"
store_priority: "NO"
store_distance: "0.037973763177421405"
store_gallery: [1]
0:  "index.jpg"
-
}-
1:  {
store_name: "SP Fort Hospital"
store_image: "index1.jpg"
store_address: "fdsfdsfds s sfds d f"
store_phone: "343423423423"
store_description: "fdsffdsf dsfdfdfdf dsf d dd"
store_url: "fdffdsfsffsff"
store_latitude: "8.484709"
store_longitude: "76.941474"
store_fb_url: "https://www.facebook.com/iglooappschool"
store_priority: "NO"
store_distance: "3.0969432359767195"
store_gallery: [2]
0:  "index1.jpg"
1:  "index3.jpg"
-
}-
-
Hotel: [1]
0:  {
store_name: "Malus"
store_image: "index3.jpg"
store_address: "Kazhakoottam"
store_phone: "124464567"
store_description: "gfdgdfgdfg d sfsgs sdfdsfdf "
store_url: "gdgdgdgdgdgddgd"
store_latitude: "8.571409"
store_longitude: "76.864965"
store_fb_url: "https://www.facebook.com/iglooappschool"
store_priority: "NO"
store_distance: "12.790990805465954"
store_gallery: [1]
0:  "index3.jpg"
-
}-
-
}-
store_premium_data: {
Hotel: [2]
0:  {
store_name: "Vishnu Restarunt"
store_image: "index.jpg"
store_address: "kattakada"
store_phone: ""
store_description: ""
store_url: ""
store_latitude: "8.489065"
store_longitude: "76.957282"
store_fb_url: "https://www.facebook.com/iglooappschool"
store_priority: "YES"
store_distance: "1.8514660234169065"
store_gallery: [1]
0:  "index3.jpg"
-
}-
1:  {
store_name: "AZAD"
store_image: "index2.jpg"
store_address: "East fort"
store_phone: "3542355452"
store_description: "sfsdfsfv dfzfdsfdsfg dsfdssdf"
store_url: "fdsfdsfdsfsdf"
store_latitude: "8.486363"
store_longitude: "76.951697"
store_fb_url: "https://www.facebook.com/iglooappschool"
store_priority: "YES"
store_distance: "2.322390609287792"
store_gallery: [1]
0:  "index2.jpg"
-
}-
-
}-
}

缺少的部分是第一个JSON中的酒店。我认为问题是当第一次运行时,缺少的部分存在。但是当第二次运行时,因为第二个json也包含&#39;酒店&#39;关键的旧酒店似乎已被覆盖。如何使其成为一个添加操作,而不是覆盖一个

编辑:

我按如下方式修改了代码,现在获得了JSON异常:

JSONObject mergedObj = new JSONObject();
                    Iterator<?> i1 = store_ordinary_data.keys();
                    Iterator<?> i2 = store_premium_data.keys();
                    while(i1.hasNext()) {
                        tmp_key = (String) i1.next();
                        mergedObj.put(tmp_key, store_ordinary_data.get(tmp_key));
                    }
                    while(i2.hasNext()) {
                        tmp_key = (String) i2.next();
                        if(mergedObj.has(tmp_key)){
                            JSONArray jsonexsarr=mergedObj.getJSONArray(tmp_key);
                            jsonexsarr.put(store_premium_data.get(tmp_key));
                            mergedObj.put(tmp_key,jsonexsarr);
                        }
                        else
                        {
                        mergedObj.put(tmp_key, store_premium_data.get(tmp_key));
                        }

                    }

代码中的问题是,当添加第二个json中的两个酒店时,它们会在第一个数组中添加一个数组。我希望将这两个酒店作为对象添加到第一个数组中。

1 个答案:

答案 0 :(得分:0)

在原始代码中,可能有两个重复的键,一个值在第二个循环中被覆盖。