解析项目目录中的一个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();
}
}

解析json实例的更多相关文章

  1. GO语言练习:构建json 和 解析JSON 实例

    本文介绍如何使用Go语言自带的库把对象转换为JSON格式,并在channel中进行传输后,并把JSON格式的信息转换回对象. 1.Go语言的JSON 库 Go语言自带的JSON转换库为 encodin ...

  2. Go语言构建json和解析json实例

    参考网址如下: https://www.cnblogs.com/fengbohello/p/4665883.html

  3. andlua,andlua发送http请求,并解析json数据

    andlua发送http请求,并解析json实例 import'cjson'import 'http'--导入cjson库url = 'https://www.baidu,com'--设置urlHtt ...

  4. Google Gson解析Json数据应用实例

    转自:http://lixigao449778967.blog.163.com/blog/static/24985164201269105928783/ 1.需要的Jar包 1) Google Gso ...

  5. (转)使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解

    使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解   本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据.2)创建json数组和解析json数组 1. 创 ...

  6. 使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解

    本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据.2)创建json数组和解析json数组 1. 创建json,从json中获取数据 #include <stdio. ...

  7. android json 解析简单实例

    Android JSON解析跟JAVA 的JSON解析原理是一样的. Android自带的JSON方式跟方便,不需要导包啥的.不深究原理了,直接上代码: public class JsonActivi ...

  8. JAVA使用Gson解析json数据,实例

    封装类Attribute: public class Attribute { private int id; private String name; private int age; public ...

  9. 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)

    在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...

随机推荐

  1. 循序渐进Python3(三) -- 3 -- 内置函数

    上一篇我们又介绍了26个Python内置函数.现回忆一下吧: 1.all 2.any 3.ascii 4.bin 5.bool 6.bytes 7.bytearray 8.callable 9.chr ...

  2. Azure DW

    1. 安装环境a. 安装环境https://www.microsoft.com/web/downloads/platform.aspx b. InputImport-Module 'C:\Progra ...

  3. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  4. MVC学习(四)几种分页的实现(3)

    在这篇MVC学习(四)几种分页的实现(2)博文中,根据URL中传入的两个参数(页码数,首页.上一页.下一页.末页的标记符)来获得对应的分页数据, 只是传入的参数太多,调用起来不太方便(标记符不能够写错 ...

  5. Windows XP SP3下编译安装openssl-1.1.0b

    软件需要: openssl-1.1.0b visual studio 2010(cn_visual_studio_2010_ultimate_x86_dvd_532347.iso) perl:Acti ...

  6. php session的存放目录,再次回顾

    session的存放位置可以通过运行时配置ini_set和函数session_save_path来设置 .session_save_path - 读取/设置当前会话的保存路径 string sessi ...

  7. 解决 label 多行间距

    使用. 一个是间距,一个是行高,但是鉴于多行,都写的话,就相当于双倍行高了.我就用行高代替间距. self.textLabel.attributedText = [self getAttributed ...

  8. XE3随笔10:TSuperType

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  9. stsdb开发指南

    摘自:http://www.iopenworks.com/Products/ProductDetails/DevelopmentGuide?proID=387 多线程问题,请参见线程安全小结 1 ST ...

  10. Java 第六章 循环结构2

    循环结构 2 会使用 for 循环结构 会在程序中使用 break 和 continue for 比 while 更简洁 什么是 for 循环 ... 语法: for 循环的语法和执行顺序 备注:2条 ...