我的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();
}
}
答案 0 :(得分:2)
首先更改您的JSON数据,它不是正确的JSON格式...
[{"音乐":" ABC"" highqualityavailable":"真""高速缓存&#34 ;: "真"}, {"音乐":" XYZ"" highqualityavailable":"假""高速缓存":"真"},{"音乐":" AAA"" highqualityavailable":"真""高速缓存&#34 ;:"真"},{"音乐":" BBB"" highqualityavailable":"假",& #34;高速缓存":"真"},{"音乐":" CCC"" highqualityavailable":"真""高速缓存":"真"}]
然后将字符串转换为JSON数组。