[英]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 我的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"}
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 该文件包含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();
}
}
First change you JSON data it's not in correct JSON format... 首先将您的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"}] [{“ 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. 然后将字符串转换为JSON数组。
More details : How to parse json parsing Using GSON in android 更多详细信息: 如何在Android中使用GSON解析json解析
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.