简体   繁体   中英

Read / Parse JSON objects from .ser file in Android

I have a file in my sdcard which extension is abc.ser and the file contain JSON objects like

{"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"}

the file contain JSON objects but not the proper format of JSON how can i read or parse it in my app i have already read the string in file but don,t know how to convert it to a 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();
                        }
                    }

First change you JSON data it's not in correct JSON format...

[{"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"}]

Then after convert string to JSON array.

More details : How to parse json parsing Using GSON in android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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