json 文件打读取

1。获取文件路径

/** BookController.class.getClassLoader().getResource("static/json/book_nav.json").getPath() 获取当期运行时的项目json文件路径*/JSONObject json = JsonResourceUtils.getJsonObjFromResource(BookController.class.getClassLoader().getResource("static/json/book_nav.json").getPath());Set<Entry<String, Object>> entrySets=json.entrySet();/** 取出json数据*/for(Entry<String, Object> entrySet: entrySets){if(entrySet.getKey().equals("bookNavs"))model.addAttribute("bookNavs",entrySet.getValue());}

2.读取json文件

package com.feilong.reptile.util;import java.io.File;
import java.io.IOException;import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;public class JsonResourceUtils{private static Logger logger = Logger.getLogger(JsonResourceUtils.class);/** filePath文件路径* @param filePath*/public static JSONObject getJsonObjFromResource(String filePath){JSONObject json = null;if (!filePath.contains(".json")) {filePath += ".json";}File file = new File(filePath);if (file.exists()) {String content=null;try {content = FileUtils.readFileToString(file, "UTF-8");} catch (IOException e) {    e.printStackTrace();logger.info("readFileToString: " + e.getMessage());}json = JSON.parseObject(content);} else {logger.info("file not exist!");}return json;}}

 3.pom 依赖

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.58</version></dependency><!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version>
</dependency>

 

转载于:https://www.cnblogs.com/jiangfeilong/p/11108316.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392975.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

16F877A和24C02通信汇编语言,pic单片机IIC通信读24C02程序例 16F877A 主频4M

#define _iic_h_//pic单片机IIC通信初始化函数声明void iiccsh(void);//pic单片机IIC通信读外围设备函数声明//功能&#xff1a;传送一个8位地址&#xff0c;返回一个8位数据unsigned char iicread(unsigned char data);//pic单片机IIC通信给外围器件发送函数声明//功能&#x…

如何从XMLHttpRequest创建自定义获取API

What is your worst nightmare?你最可怕的噩梦是什么&#xff1f; That sounded dark, but it’s not a rhetorical question. I really want to know because I am about to tell you mine. Along the way, we will learn some things like how the fetch API works and als…

leetcode637. 二叉树的层平均值

给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。示例 1&#xff1a;输入&#xff1a;3/ \9 20/ \15 7 输出&#xff1a;[3, 14.5, 11] 解释&#xff1a; 第 0 层的平均值是 3 , 第1层是 14.5 , 第2层是 11 。因此返回 [3, 14.5, 11] 。/*** Definition for a b…

5.3 上午

观看英语课程——《恋练有词》 学习Linux 转载于:https://www.cnblogs.com/bgd140206110/p/6801164.html

AD库转换为KiCAD库的方法

AD库转换为KiCAD库的方法 参照博主另外一篇文档&#xff1a; AD转换为KiCAD的方法&#xff0c;点击此处转载于:https://www.cnblogs.com/zhiqiang_zhang/p/11109560.html

遗传算法求解装箱问题c语言,求解装箱问题的遗传算法-南昌航空大学期刊网.pdf...

求解装箱问题的遗传算法-南昌航空大学期刊网1998 2 Journal of Nanchang Institute of Aeronautical Technology 21998方 平    李 娟( 南昌航空工业学院)  ( 西北工业大学): ( Bin Packing) ,, , D( irst it De-creasing) ,: ; ; ;: TP301. 6( )( Bin Packing) , :1 2 …

mysql索引随记

为什么80%的码农都做不了架构师&#xff1f;>>> 先了解下Btree&#xff1a;https://my.oschina.net/u/3646190/blog/1593094 为什么每个数据项&#xff0c;即索引字段要尽量的小&#xff0c;比如int占4字节&#xff0c;要比bigint8字节少一半&#xff1f; 通过上面…

leetcode79. 单词搜索(回溯算法)

给定一个二维网格和一个单词&#xff0c;找出该单词是否存在于网格中。 单词必须按照字母顺序&#xff0c;通过相邻的单元格内的字母构成&#xff0c;其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。 示例: board [ [‘A’,‘…

react钩子_迷上了钩子:如何使用React的useReducer()

react钩子So the React Conference just happened and as always something new happened. Hooks happened! The React team talked about suspense, lazy loading, concurrent rendering, and hooks :D.因此&#xff0c;React会议刚刚发生&#xff0c;并且一如既往地发生了一些…

开发注意事项

明确需求 - 沟通 - 定好上下游接口 次序乱不得转载于:https://www.cnblogs.com/zslzz/p/6802437.html

c语言写桌面程序unity,Unity和iOS原生界面交互示例

注意上面的Main方法中出现的UnityAppController&#xff0c;该类就是作为控制类来实现Unity在iOS上显示的功能&#xff0c;在Main方法中就是将该控制器作为参数传递&#xff0c;即Main方法之后就会进入该类执行。所以这是我们进入到UnityAppController.mm&#xff0c;来查看该类…

oracle审计实施

1、语句审计 Audit session; Audit session By ; 与instance连接的每个会话生成一条审计记录。审计记录将在连接时期插入并且在断开连接时期进行更新。 保留有关会话的信息比如连接时期断开连接时期处理的逻辑和物理I/O&#xff0c;以及更多信息将存储在单独一条审计 记录中…

JPDA 架构研究5 - Agent利用环境指针访问VM (内存管理篇)

引入&#xff1a; 我们在前面说到JVMTI的客户端Agent,又提到Agent通过环境指针来访问VM。这里就来看看环境指针到底有多大的访问VM的能力。 分类1&#xff1a;内存管理 a.Allocate. 分配内存 jvmtiError Allocate(jvmtiEnv* env,jlong size,unsigned char** mem_ptr) size:分配…

leetcode94. 二叉树的中序遍历(dfs)

给定一个二叉树&#xff0c;返回它的中序 遍历。示例:输入: [1,null,2,3]1\2/3输出: [1,3,2]代码 /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val x; }* }*/ class …

vtk删除一个actor_如何构建一个基于actor的简单区块链

vtk删除一个actorScalachain is a blockchain built using the Scala programming language and the actor model (Akka Framework).Scalachain是使用Scala编程语言和参与者模型( Akka Framework )构建的区块链。 In this story I will show the development process to build…

java枚举的简单介绍

1.枚举&#xff0c;enum关键字&#xff0c;相当于public final static. 2.举例&#xff1a; 首先定义了一个名为spiciness的枚举类型。 public enum Spiciness {NOT, MILD, MEDIUM, HOT, FLAMING } 再来测试一下enum&#xff0c;这个测试方法表明它有tostring()方法&#xff0…

浏览器中插入富文本编辑器

常用的富文本编辑器有CKEditor、UEEditor、TinyEditor、KindEditor等、以下以kindeditor编辑器的使用为例。 1.官网下载KindEditor编辑器http://kindeditor.net/down.php&#xff0c; 当前最新版本为4.1.11&#xff0c;解压缩后放入项目的static目录&#xff0c;作为js插件引用…

获取Extjs文本域中的内容

经常在Ext.select()和Ext.query()等问题上纠结&#xff0c;今天终于有了点新认识&#xff1a; 需求&#xff0c;假设我们的页面上有个panel ,其id为clusterstab_edit_details,这个panel的内部有个textarea,这个textarea的name为editDetails_Description,那么我们有多少方法可以…

android触摸指纹会触发按键功能,Android P新特性:利用触摸指纹识别器能阻止手机息屏...

设想你正在阅读手机上的文章&#xff0c;突然间显示屏变暗了一点。显然&#xff0c;你设置的30秒或1分钟超时息屏对于常规使用来说还可以&#xff0c;但对于阅读纯文本片段&#xff0c;还远远不够。因此&#xff0c;这时你会轻触屏幕&#xff0c;可能会上下滑动&#xff0c;以防…

leetcode37. 解数独(hashmap+回溯)

编写一个程序&#xff0c;通过已填充的空格来解决数独问题。 一个数独的解法需遵循如下规则&#xff1a; 数字 1-9 在每一行只能出现一次。 数字 1-9 在每一列只能出现一次。 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。 空白格用 ‘.’ 表示。 Note: 给定的数独序…