[工具类] 读取解析json文件
读取json文件并转换为字符串
/**
* 通过本地文件访问json并读取
*
* @param path:json文件路径
* @return:json文件的内容
*/
public static String ReadFile(String path) {
StringBuffer laststr = new StringBuffer();
File file = new File(path);// 打开文件
BufferedReader reader = null;
try {
FileInputStream in = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));// 读取文件
String tempString = null;
while ((tempString = reader.readLine()) != null) {
laststr = laststr.append(tempString);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException el) {
}
}
}
return laststr.toString();
}
解析json(fastjson)
public static void main(String args[]) {
String filePath = "D:/data.json";
String jsonString = ReadFile(filePath);
JSONArray jsonArr = JSON.parseArray(jsonString);
Map<String, Object> resultMap = new HashMap<String, Object>();
for (int i = 0; i < jsonArr.size(); i++) { Map data_map = (Map) jsonArr.get(i);//本次循环获取数据
Integer customer_id = (Integer) data_map.get("customer_id");
String customer_email = (String) data_map.get("customer_email");
String customer_firstname = (String) data_map.get("customer_firstname");
String customer_lastname = (String) data_map.get("customer_lastname"); String sku = (String) data_map.get("sku");
String created_at = (String) data_map.get("created_at"); Map level_1_map = (Map) resultMap.get(customer_id.toString());
if (null == level_1_map || level_1_map.size() <= 0) {
level_1_map = new HashMap<String, Object>();
level_1_map.put("customer_id", customer_id);
level_1_map.put("customer_email", customer_email);
level_1_map.put("customer_firstname", customer_firstname);
level_1_map.put("customer_lastname", customer_lastname);
} List list = level_1_map.get("list") == null ? new ArrayList<T>() : (List) level_1_map.get("list");
Map listMap = new HashMap<String, Object>();
listMap.put("sku", sku);
listMap.put("created_at", created_at);
list.add(listMap);
level_1_map.put("list", list); resultMap.put(customer_id.toString(), level_1_map);
}
System.out.println(JSON.toJSONString(resultMap)); }
[工具类] 读取解析json文件的更多相关文章
- scala读取解析json文件
import scala.util.parsing.json.JSON._ import scala.io.Source object ScalaJsonParse { def main(args: ...
- Java解析JSON文件的方法
http://blog.sina.com.cn/s/blog_628cc2b70101dydc.html java读取文件的方法 http://www.cnblogs.com/lovebread/ar ...
- 文件读取工具类读取properties文件
1.创建工具类 import java.io.IOException; import java.util.Properties; /** * * 类名称:PropertiesUtil * 类描述: 文 ...
- Android--------使用gson解析json文件
##使用gson解析json文件 **json的格式有两种:** **1. {}类型,及数据用{}包含:** **2. []类型,即数据用[]包含:** 下面用个例子,简单的介绍gson如何解析jso ...
- 解析HTML文件 - 运用SgmlReader类来解析HTML文件
运用.NET Framework类来解析HTML文件.读取数据并不是最容易的.虽然你可以用.NET Framework中的许多类(如StreamReader)来逐行解析文件,但XmlReader提供的 ...
- ZIP解压缩文件的工具类【支持多级文件夹|全】
ZIP解压缩文件的工具类[支持多级文件夹|全] 作者:Vashon 网上有非常多的加压缩演示样例代码.可是都仅仅是支持一级文件夹的操作.假设存在多级文件夹的话就不行了. 本解压缩工具类经过多次检查及重 ...
- 安卓解析JSON文件
安卓解析JSON文件 根据JOSN文件的格式,文件只有两种数据,一是对象数据,以 {}为分隔,二是数组,以[]分隔 以下介绍安卓如何解析一个JSON文件,该文件存放在assets目录下,即:asset ...
- 读取本地json文件,转出为指定格式json 使用Base64进行string的加密和解密
读取本地json文件,转出为指定格式json 引用添加Json.Net 引用命名空间 using Newtonsoft.Json //读取自定目录下的json文件StreamReader sr = ...
- boost::property_tree读取解析.xml文件
boost::property_tree读取解析.xml文件 1)read_xml 支持中文路径 boost::property_tree::wptree wpt; std::locale:: ...
随机推荐
- [FFT/NTT/MTT]总结
最近重新学了下卷积,简单总结一下,不涉及细节内容: 1.FFT 朴素求法:$Coefficient-O(n^2)-CoefficientResult$ FFT:$Coefficient-O(nlogn ...
- [清橙A1210]光棱坦克
[清橙A1210]光棱坦克 题目大意: 平面上放置了\(n(n\le7000)\)个反射装置,光纤将从某个装置出发,在经过一处装置时发生反射,若经过的装置坐标依次为\((x_1,y_1),(x_2,y ...
- 前端性能优化 —— 减少HTTP请求
简要:对于影响页面呈选 的因素有3个地方:服务器连接数据库并计算返回数据 , http请求以及数据(文件)经过网络传输 , 文件在浏览器中计算渲染呈选: 其中大约80%的时间都耗在了http请求上,所 ...
- 【2013Esri全球用户大会精彩案例】GIS for Philadelphia’s Finest --费城警用GIS
行业领域:警务 拥有6000多警员和侦探的费城警察局,历时三年,搭建了费城警用GIS,目前可以对每天发生的事进行汇总(如图1),并可动态的进行热点分析(如图2).区域指挥官可方便的查看警员的活动 ...
- Android开发中遇到的问题(三)——eclipse创建android项目无法正常预览布局文件
一.问题描述 今天使用SDK Manager将Android SDK的版本更新到了Android 5.1的版本,eclipse创建android项目时,预览activity_main.xml文件时提示 ...
- 体验jQuery和AngularJS的不同点以及AngularJS的迷人之处
本篇通过jQuery和Angular两种方式来实现同一个实例,从而体验两者的不同点以及AngularJS的迷人之处. 首先当然需要引用jquery.js和angular.js文件. ■ 使用jQuer ...
- android:ProgressDialog控件
ProgressDialog 和 AlertDialog 有点类似,都可以在界面上弹出一个对话框,都能够屏蔽 掉其他控件的交互能力.不同的是,ProgressDialog 会在对话框中显示一个进度条, ...
- Java知识回顾 (4)Java包装类
一. Java Number 一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double 等. 然而,在实际开发过程中,我们经常会遇到需要使用对象,而不是内置 ...
- ArcGIS Pro 中不可用的工具
有些可用于 ArcMap 之类的其他 ArcGIS Desktop 应用程序的地理处理工具在 ArcGIS Pro 中不可用.用于处理 ArcGIS Pro 所不支持的数据格式的地理处理工具已被移除, ...
- Scala:Functional Objects
先上代码 class FunctionalObjects(var _x: Int, var _y: Int) { require(_x > 0) require(_y > 0) def t ...