来自JSON的Android ListView,其中包含Space in string

时间:2018-05-25 17:18:46

标签: android json listview

我有这个listview,我从字符串中的json填充它,它完美地工作但是当这个json包含空格onclick事件应用程序崩溃时:

(我评论了应用程序崩溃的部分代码和图像)

final ListView mylist = (ListView) findViewById(R.id.listView1);
        List<Map<String, String>> data = new ArrayList<Map<String, String>>();
        JSONObject jObj = null;
        String s = testopass;
        int counter = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ':') {
                counter++;
            }
        }
        if (counter > 0) {
            try {
                try {
                    jObj = new JSONObject(testopass);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Iterator<String> iter = jObj.keys();
                while (iter.hasNext()) {
                    String key = iter.next();
                    try {
                        Object value = jObj.get(key);
                        Map<String, String> datum = new HashMap<String, String>(2);
                        datum.put("nome",value.toString());
                        datum.put("cf", key.toString());
                        data.add(datum);
                    } catch (JSONException e) {
                        // Something went wrong!
                    }
                }
            } catch (Exception er) {

            }

            adapter = new SimpleAdapter(getApplicationContext(), data,
                    android.R.layout.simple_list_item_2,
                    new String[]{"nome", "cf"},
                    new int[]{android.R.id.text1,
                            android.R.id.text2});

            mylist.setAdapter(adapter);

            mylist.setClickable(true);

            mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                    Object o = mylist.getItemAtPosition(position);
                    JSONObject jObj = null;
                    try {
                        jObj = new JSONObject(o.toString()); //this return error and go to catch
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    String cane = "";
                    try {
                        cane = jObj.getString("cf"); //crash is here because jObj is empty
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }  


                }

            });

        testopass is a string: `{"12345674":"SPACE CRASH"}` 

应用程序的结果是:

enter image description here

我已经停止了代码:

enter image description here

崩溃是:

org.json.JSONException: Unterminated object at character 13 of {nome=SPACE CRASH, cf=12345674}

如何解决这个问题?我非常非常新的android dev

我有部分解决方案

 jObj = new JSONObject(o.toString().replace("nome=","nome=\"").replace(",","\","));

这段代码没有崩溃,但我认为有更好的解决方案吗?

1 个答案:

答案 0 :(得分:1)

您没有将适配器传递给JSONObject。这意味着您不应该尝试在onItemClick()方法中对其进行解码。您实际上正在传递ArrayListMap<String, String>个对象。这意味着您需要转换为该对象。

尝试这样的事情:

@Override
public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
    String cane = "nothing";
    try {
        String keyName = "cf";

        Map<String, String> map = (Map<String, String>)parent.getItemAtPosition(position);
        if(map.containsKey(keyName)){
            cane = map.get(keyName);
        }
        else{
            Log.e("onItemClick", "Upps no value for: " + keyName);
        }
        } catch (Exception e) {
            e.printStackTrace();
        }  
    }
});

<强> ___________________________________________________

注意:

一般情况下,建议不要在一个方法中使用多个try..catch块,除非您在代码中执行某些操作以防止代码继续使用错误数据或{{1对象!您的代码中有几个示例,您只需记录错误,但随后(如果发生错误)将使用null对象在该方法中继续。例如:

null