簡體   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