从Android中的.ser文件中读取/解析JSON对象

时间:2015-09-12 15:44:45

标签: java android json serialization deserialization

我的sdcard中有一个文件,扩展名为abc.ser,文件包含JSON对象,如

{"music":"abc","highqualityavailable":"true","cacheable":"true"}
{"music":"xyz","highqualityavailable":"false","cacheable":"true"}
{"music":"aaa","highqualityavailable":"true","cacheable":"true"}
{"music":"bbb","highqualityavailable":"false","cacheable":"true"}
{"music":"ccc","highqualityavailable":"true","cacheable":"true"}

该文件包含JSON对象,但没有正确的JSON格式我如何在我的应用程序中读取或解析它我已经读过文件中的字符串但是不知道如何将其转换为POJO

 String root = Environment.getExternalStorageDirectory().getAbsolutePath();

                File file = new File(root+"/tmp/playerSongsString.ser");

                if (file.exists()) {


                    FileInputStream stream = null;

                    try {

                        stream = new FileInputStream(file);
                        FileChannel fc = stream.getChannel();
                        MappedByteBuffer bb =    fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
                      /* Instead of using default, pass in a decoder. */
                        jString = Charset.defaultCharset().decode(bb).toString();

                        JSONObject object = new JSONObject(jString);

                        Log.d("json:",jString);




                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            stream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

1 个答案:

答案 0 :(得分:2)

首先更改您的JSON数据,它不是正确的JSON格式...

[{"音乐":" ABC"" highqualityavailable":"真""高速缓存&#34 ;: "真"}, {"音乐":" XYZ"" highqualityavailable":"假""高速缓存":"真"},{"音乐":" AAA"" highqualityavailable":"真""高速缓存&#34 ;:"真"},{"音乐":" BBB"" highqualityavailable":"假",& #34;高速缓存":"真"},{"音乐":" CCC"" highqualityavailable":"真""高速缓存":"真"}]

然后将字符串转换为JSON数组。

更多详情:How to parse json parsing Using GSON in android