2

我需要帮助将 JSON 响应转换为 UTF-8。当我使用 UTF-8(没有 BOM)保存我的 .json 文件时,一切都运行良好。当我只使用 UTF-8 保存文件时,它不起作用,应用程序在获取 JSON 时崩溃。

底部的 Logcat

来源:

..

    public class JSONPARSER extends ListActivity {


        private static String url = "http://profusum.se/neger.json";


        private static final String TAG_CONTACTS = "messages";
        private static final String TAG_NAME = "namn";
        private static final String TAG_ID = "id";
        private static final String TAG_KIK = "facebook";
        private static final String TAG_IMGURL = "img";


        JSONArray contacts = null;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.drivers);


            ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();


            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.getJSONFromUrl(url);

            try {


                contacts = json.getJSONArray(TAG_CONTACTS);

                for(int i = 0; i < contacts.length(); i++){
                    JSONObject c = contacts.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_NAME, name);


                    contactList.add(map);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            ListAdapter adapter = new SimpleAdapter(this, contactList,
                    R.layout.list_item,
                    new String[] { TAG_NAME,}, new int[] {
                            R.id.inboxName, });

            setListAdapter(adapter);

            ListView lv = getListView();

            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    String name = ((TextView) view.findViewById(R.id.inboxName)).getText().toString();

                    Intent in = new Intent(getApplicationContext(),                 SingleMenuItemActivity.class);
                    in.putExtra(TAG_NAME, name);
                    startActivity(in);

                }
            });



        }
}

我现在已经努力寻找这个,我猜这很简单..但我无法弄清楚。

05-01 21:13:08.213: E/JSON Parser(27153): Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject

概括

当我在 Notepad++ 中使用“UTF-8(无 BOM)”保存文件时,解析成功。但我在应用程序中得到了这些奇怪的符号。(见示例)

但是当我在 Notepad++ 中使用“UTF-8”保存文件时,JSON 会给我正确的符号,但 Android 应用程序不会解析它。

Example
Tim Billström
4

2 回答 2

1

根据RFC 4627,JSON 的默认编码应该是 UTF-8。并且UTF-8不需要BOM,实际上不鼓励使用(严格来说:“UTF-8 既不需要也不推荐使用 BOM”)!

所以你应该做的是将你的文件保存为“UTF-8(不带BOM)”(这应该是默认设置,“UTF-8(带BOM)”应该是特殊选项。

如果您正在使用此博客文章JSONParser中的类(或类似内容),那么您应该修复该代码:它硬编码 ISO-8859-1 编码,这是错误的(除非您特别知道您需要它)。

理想情况下,您应该尊重服务器在 HTTP 标头中告诉您的编码。或者,您可以假定指定的默认值(即 UTF-8)。

于 2013-05-02T07:21:14.163 回答
0

您应该使用带有 UTF-8 编码的 JSON 而不使用 BOM,否则您会在文件的开头得到这些奇怪的字符,并且您无法解析它。

另请参阅此问题(关于 PHP 但相同类型的问题)

于 2013-05-01T20:55:04.327 回答