0

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();
                        }
                    }
shakil.k
  • 1,623
  • 5
  • 17
  • 27
  • It will simplify your task a lot if you could fix these files to have correct JSON formatted data. – Egor Sep 12 '15 at 15:52
  • yes i know but the file is created by some other app and i have no control over it – shakil.k Sep 12 '15 at 15:53
  • If you are sure file size is small, you can simply use `JSONObject object = new JSONObject("["+jString+"]");` – makata Sep 12 '15 at 16:48
  • To parse the JSONObject to POJO you can refer [this tutorial.](http://www.vogella.com/tutorials/JavaLibrary-Gson/article.html) – makata Sep 12 '15 at 16:57

2 Answers2

2

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

Community
  • 1
  • 1
Raj Turakhia
  • 245
  • 2
  • 8
0

This python code is only for converting .ser file into .json file

Files : Opening files from this repository

with open('first-letters-root.ser', encoding="utf8") as f:
    w = f.read()
    
y = w.split('i:')[1:]
d = {}

for k,i in enumerate(y):
    j = i.split(';')[1].split(':')
    d[k] = j[-1]

import json
with open('first-letters-root.json', 'w') as f:
    json.dump(d, f)

info = open('first-letters-root.json')
res = json.load(info)
print(res)