rixiang

解析项目目录中的一个json文件,将之转化为List的一个方法。

package com.miracles.p3.os.util;

import com.miracles.p3.os.mode.VideoBean;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SevilinMa on 2016/3/17.
 */
public class ConfigHelper {
    private static ConfigHelper configHelper;

    public final String LIB_FILE = "./lib.json";
    public final String CONFIG_FILE = "./config.json";

    private ConfigHelper(){
    }

    public static ConfigHelper contextConfigHelper(){
        if(configHelper == null){
            configHelper = new ConfigHelper();
        }
        return configHelper;
    }

    public List<VideoBean> getHelperVideoList(){
        List<VideoBean> liblist = new ArrayList<>();
        String jsontext = FileUtil.readFile(LIB_FILE);
        try {
            JSONObject jsonObject = new JSONObject(jsontext);
            JSONArray libs = jsonObject.getJSONArray("helplib");
            JSONObject item;
            VideoBean bean;
            for(int i=0;i<libs.length();i++){
                item = libs.getJSONObject(i);
                bean = new VideoBean();
                bean.setTitle(item.getString("title"));
                bean.setVideoPath(item.getString("path"));
                liblist.add(bean);
            }
        }catch (Exception e){
            e.printStackTrace();
            liblist.clear();
        }
        return liblist;
    }

}

 

 

 

package com.miracles.p3.os.util;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

/**
 * Created by SevilinMa on 2016/3/17.
 * 文件Util类,用于文件处理相关操作
 */
public class FileUtil {
    /**
     * 读取文件所有数据
     * @param path 文件路径
     * @return 返回String
     */
    public static String readFile(String path){
        StringBuilder sb = new StringBuilder();
        try(Stream<String> stream = Files.lines(Paths.get(path))){
            stream.forEachOrdered(sb::append);
        }catch (Exception e){
            e.printStackTrace();
        }
        return sb.toString();
    }
}

 

posted on 2016-03-18 14:06  eques  阅读(393)  评论(0编辑  收藏  举报

分类:

技术点:

相关文章:

  • 2021-11-20
  • 2021-12-03
  • 2021-12-19
  • 2022-12-23
  • 2022-01-04
  • 2021-11-17
  • 2021-06-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
  • 2021-07-11
  • 2021-11-16
相关资源
相似解决方案