【问题标题】:Android ListView from JSON with Space in string来自JSON的Android ListView,字符串中有空格
【发布时间】:2018-05-25 17:18:45
【问题描述】:

我有这个列表视图,我从字符串中的 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"}` 

应用中的结果是:

我已经停止了代码:

崩溃是:

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

如何解决这个问题?我在 android 开发中非常新

我有一个部分解决方案

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

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

【问题讨论】:

  • 您可以使用 GSON (github.com/google/gson) 来解析 JSON。这将使您的工作更轻松
  • 问题可能出在这里 datum.put("cf", key.toString());由于没有正确放置数据,因此当您尝试将其传递到下方以将其传递给 cane 时,您没有获得该值,因此 cane 为空

标签: android json listview


【解决方案1】:

您没有向适配器传递JSONObject。这意味着您不应该尝试在 onItemClick() 方法中对其进行解码。您实际上是在传递带有Map&lt;String, String&gt; 对象的ArrayList。这意味着您需要强制转换为该对象。

试试这样的:

@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除非您正在代码中执行某些操作以防止代码继续处理错误数据或null 对象!您的代码中有几个示例,您只需在其中记录错误,然后(如果发生错误)使用 null 对象继续该方法。示例:

try {
    //if an **error** occurs **HERE**....
    jObj = new JSONObject(testopass);
    } 
    catch (JSONException e) {
        // You must do something **HERE** to get things straight **OR** **EXIT**
        e.printStackTrace();
    }
    //... you continue **HERE** with a **null** object jObj!!
    Iterator<String> iter = jObj.keys();
    while (iter.hasNext()) { 
    //....

【讨论】:

  • 感谢您的宝贵时间,完美的解决方案 =)
猜你喜欢
  • 1970-01-01
  • 2012-03-13
  • 2021-02-21
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多