【问题标题】:Android - Convert JSON to UFT-8Android - 将 JSON 转换为 UTF-8
【发布时间】:2013-04-25 20:28:03
【问题描述】:

我需要帮助将 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

【问题讨论】:

    标签: android json utf-8


    【解决方案1】:

    根据RFC 4627,JSON 的默认编码应该是 UTF-8。而UTF-8 doesn't need a BOM 实际上不鼓励这样做(严格来说:“UTF-8 既不需要也不建议使用 BOM”)!

    因此,您应该将文件保存为“UTF-8(无 BOM)”(这应该是默认设置,而“UTF-8(带有 BOM)”应该是特殊选项。

    如果您使用的是 JSONParserfrom this blog post(或类似的东西),那么您应该修复该代码:它硬编码 ISO-8859-1 编码,这是错误(除非您明确知道自己需要它)。

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

    【讨论】:

    • 哦,我忘了把“iso-8859-1”改成“utf-8”。感谢您的回答!
    • @TimLilleSkuttBillström:不客气。以后请提及您使用的任何第三方库/类(因为 Android API 中没有 JSONParser 类)。
    【解决方案2】:

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

    另见this question (about PHP but same kind of issue)

    【讨论】:

    • 我已经编辑了问题,请看底部。提前致谢
    猜你喜欢
    • 2015-12-28
    • 2017-09-24
    • 2015-04-29
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    相关资源
    最近更新 更多