I would like to know using Android Studio how to load & read a json file saved in the sdcard?
The json file contains simple json objects. Also after getting the data how can i parse it & set it as pojo.
我想知道如何使用Android Studio加载和读取保存在SD卡中的json文件? json文件包含简单的json对象。获取数据后,我如何解析它并将其设置为pojo。
Json example: { "name":"Abcd", "id":"xyz" }
Json示例:{“name”:“Abcd”,“id”:“xyz”}
Thanks.
1 个解决方案
#1
1
Reading file:
InputStream iStream = new FileInputStream("filename.json");
BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));
String line;
String content = "";
while((line = bReader.readLine()) != null) {
content += line;
}
the easiest way to convert to POJO is to use one of the popular libraries (Gson, Jackson, etc). Using Gson:
转换为POJO的最简单方法是使用一个流行的库(Gson,Jackson等)。使用Gson:
Gson gson = new GsonBuilder().create();
gson.fromJson(content, Pojo.class);
#1
1
Reading file:
InputStream iStream = new FileInputStream("filename.json");
BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));
String line;
String content = "";
while((line = bReader.readLine()) != null) {
content += line;
}
the easiest way to convert to POJO is to use one of the popular libraries (Gson, Jackson, etc). Using Gson:
转换为POJO的最简单方法是使用一个流行的库(Gson,Jackson等)。使用Gson:
Gson gson = new GsonBuilder().create();
gson.fromJson(content, Pojo.class);