这里我从存储了一些json字符串的txt文件中读取出来,并且转化成我已经定义过的H_TOPIC对象
1.自己已经定义过的一个实例类(例如 H_TOPIC类)
"id":"111111"
"TITLE":"标题"
"CONTENT":"内容"
"MEDIALIST":"媒体列表"
2.从文件中读取json并转化
//从文件中获取TOPIC集合数据
public static List<H_TOPIC> getTopicFromFile(String pathName) {
List<H_TOPIC> topicList = new ArrayList<>();
try {
List<String> topicStr = FileUtils.readLines(new File(pathName), "UTF-8");
for (String str : topicStr) {
// "id":"111111" "TITLE":"标题" "CONTENT":"内容" "MEDIALIST":"媒体列表"
H_TOPIC topic = (H_TOPIC) JsonUtil.jsonToObj(str, H_TOPIC.class);
topicList.add(topic);
}
} catch (IOException e) {
e.printStackTrace();
}
return topicList;
|
|