Java maven项目中读取json资源文件

应用场景

Java maven项目中读取json资源文件_第1张图片
springboot maven项目,想读取resources资源目录下自定义的configuration.json文件时,通过下面的代码在本机测试时可以读取

try{	//filename="configuration.json"
    URL url = JsonResourceUtils.class.getResource(filename);
    String path = url.getPath();

    File file = new File(path);
    if (file.exists()){
        String content = FileUtils.readFileToString(file,"UTF-8");
        json = JSON.parseObject(content);
    }else{
        log.info("[{}] file not exist",filename);
    }
}catch (Exception e){
    log.error("read file to string error",e);
}

但是打成Jar包的时候又没办法读取了,原因在ResourceUtils无法读取json文件中得以解决

解决问题

但是我想做一个通用的读取classpath中json文件的方法

用到的包:

  • fastjson
  • slf4j

Configuration.json内容

{
  "resultCode":250,
  "tags": [
    "Java",
    "Cpp",
    "JS"
  ]
}

JsonResourceUtils.java

package com.hong.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Slf4j
public class JsonResourceUtils {

    public JsonResourceUtils() {
    }


    public static JSONObject getJsonObjFromConfiguration(){
        String FILENAME = "configuration.json";
        return getJsonObjFromResource(FILENAME);
    }

    public static JSONObject getJsonObjFromResource(String filename){
        JSONObject json = null;

        if (!filename.contains(".json")){
            filename += ".json";
        }

        try{
            InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
            if (inputStream != null){
                StringBuilder sb = new StringBuilder();
                InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
                BufferedReader bfReader = new BufferedReader(reader);
                String content = null;
                while((content = bfReader.readLine()) != null){
                    sb.append(content);
                }
                bfReader.close();
                json = JSON.parseObject(sb.toString());
            }else{
                log.info("[{}] file not exist",filename);
            }

        }catch (Exception e){
            log.error("read file to string error",e);
        }

        return json;
    }

    public static String[] parseJsonArray(JSONObject json,String arg){
        JSONArray jsonArray = (JSONArray)json.get(arg);
        List<String> list = (List<String>)JSONArray.parseArray(jsonArray.toString(),String.class);
        return list.toArray(new String[list.size()]);
    }

    public static void main(String[] args) {
        JSONObject json =  getJsonObjFromConfiguration();
        Integer resultCode =  (Integer) json.get("resultCode");
        System.out.println(resultCode);

        String[] strings = parseJsonArray(json,"tags");
        for (String string:strings){
            System.out.print(string+" ");
        }
    }
}

最终结果:

你可能感兴趣的:(Java,json,java,spring,boot,spring)