java读取本地json文件

package com.ht.projectdemo.common;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import java.util.Map;

/**
 * 对json文件的读操作
 */
public class LocalJSONFileUtil {
    /**
     * 根据key获取json
     * @param filepath 文件路径
     * @param key json的key
     * @return
     */
    public static Object getJSONByKey(String filepath, String key){
       String text =  LocalJSONFileUtil.getFileText(filepath);
        JSONObject object = new JSONObject();
       if (StringUtils.isNoneBlank(text)){
            object = parseObject(text);
       }
        return object.get(key);
    }

    /**
     * 将json 字符串转换为json对象
     */
    public static JSONObject parseObject(String text) {
        return JSONObject.parseObject(text);
    }

    /**
     * 获取文件内文本
     * @param filepath
     * @return
     */
    public static String getFileText(String filepath){
        StringBuffer sb = new StringBuffer();
        try{
            ClassPathResource classPathResource = new ClassPathResource(filepath);
            Reader reader = new InputStreamReader(classPathResource.getInputStream());
            int ch = 0;
            while ((ch = reader.read())!=-1){
                sb.append((char)ch);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString().replaceAll("\r\n","");
    }

}

class Text{

    public static void main(String[] args) {
        System.out.println("获取json文本,也可以获取txt文本");
        String textJOSN = LocalJSONFileUtil.getFileText("templates/localJSON.json");
        System.out.println(textJOSN); //{  "key1": "value1",  "key2": "value2",  "key3": [{"key1": "value1"},{"key2": "value2"}]}
        String txt = LocalJSONFileUtil.getFileText("templates/localJSON.txt"); // 测试文本
        System.out.println(txt);
//        LocalJSONFileUtil.getJSONByKey()
        System.out.println("=============通过key获取对象=================");
        String key2 = (String) LocalJSONFileUtil.getJSONByKey("templates/localJSON.json","key2");
        System.out.println("key2:"+key2); // key2:value2
        List<Map<String,Object>> key3 = (List<Map<String, Object>>) LocalJSONFileUtil.getJSONByKey("templates/localJSON.json","key3");
        System.out.println(key3); // [{"key1":"value1"},{"key2":"value2"}]
        Map<String,Object> key4 = (Map<String, Object>) LocalJSONFileUtil.getJSONByKey("templates/localJSON.json","key4");
        System.out.println(key4); //{"key1":"value1"}

        System.out.println("=========获取整体json对象=============");
        Map<String,Object> objectMap = LocalJSONFileUtil.parseObject(LocalJSONFileUtil.getFileText("templates/localJSON.json"));
        System.out.println(objectMap.get("key1")); // value1

        Map<String,Object> objectMap2 = JSONObject.parseObject(LocalJSONFileUtil.getFileText("templates/localJSON.json"));
        System.out.println(objectMap2.get("key1"));

        System.out.println("向文本内写入值,一般工作中没有这个需求,思路也很简单,封装个方法,参数为,路径,key,value, 通过路径读取JSON到程序,通过map的put方法向json中put值,最后将数据再次写入到JSON文件内就行了");

    }
}

 <!-- json  -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.39</version>
        </dependency>

        <!-- common  -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.5</version>
        </dependency>
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值