繁体   English   中英

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

[英]Read / Parse JSON objects from .ser file in Android

我的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();
                        }
                    }

首先将您的JSON数据更改为不正确的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数组。

更多详细信息: 如何在Android中使用GSON解析json解析

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM