读取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文件的更多相关文章

  1. scala读取解析json文件

    import scala.util.parsing.json.JSON._ import scala.io.Source object ScalaJsonParse { def main(args: ...

  2. Java解析JSON文件的方法

    http://blog.sina.com.cn/s/blog_628cc2b70101dydc.html java读取文件的方法 http://www.cnblogs.com/lovebread/ar ...

  3. 文件读取工具类读取properties文件

    1.创建工具类 import java.io.IOException; import java.util.Properties; /** * * 类名称:PropertiesUtil * 类描述: 文 ...

  4. Android--------使用gson解析json文件

    ##使用gson解析json文件 **json的格式有两种:** **1. {}类型,及数据用{}包含:** **2. []类型,即数据用[]包含:** 下面用个例子,简单的介绍gson如何解析jso ...

  5. 解析HTML文件 - 运用SgmlReader类来解析HTML文件

    运用.NET Framework类来解析HTML文件.读取数据并不是最容易的.虽然你可以用.NET Framework中的许多类(如StreamReader)来逐行解析文件,但XmlReader提供的 ...

  6. ZIP解压缩文件的工具类【支持多级文件夹|全】

    ZIP解压缩文件的工具类[支持多级文件夹|全] 作者:Vashon 网上有非常多的加压缩演示样例代码.可是都仅仅是支持一级文件夹的操作.假设存在多级文件夹的话就不行了. 本解压缩工具类经过多次检查及重 ...

  7. 安卓解析JSON文件

    安卓解析JSON文件 根据JOSN文件的格式,文件只有两种数据,一是对象数据,以 {}为分隔,二是数组,以[]分隔 以下介绍安卓如何解析一个JSON文件,该文件存放在assets目录下,即:asset ...

  8. 读取本地json文件,转出为指定格式json 使用Base64进行string的加密和解密

    读取本地json文件,转出为指定格式json   引用添加Json.Net 引用命名空间 using Newtonsoft.Json //读取自定目录下的json文件StreamReader sr = ...

  9. boost::property_tree读取解析.xml文件

    boost::property_tree读取解析.xml文件 1)read_xml 支持中文路径  boost::property_tree::wptree wpt;    std::locale:: ...

随机推荐

  1. Python学习——collections系列

    一 ,计数器(counter) Counter是对字典类型的补充,用于追踪值得出现次数 ps:具备字典的所有功能 + 自己的功能 例: >>> from collections im ...

  2. jquery.pagination.js添加跳转页

    原作者github地址:https://github.com/gbirke/jquery_pagination 在这基础上加入了跳转到指定页. 修改后的jquery.pagination.js /** ...

  3. 8.6 正睿暑期集训营 Day3

    目录 2018.8.6 正睿暑期集训营 Day3 A 亵渎(DP) B 绕口令(KMP) C 最远点(LCT) 考试代码 A B C 2018.8.6 正睿暑期集训营 Day3 时间:5h(实际) 期 ...

  4. ecshop jquery 冲突

    遇到冲突在脚本前面加上这句 $(function() { window.__Object_toJSONString = Object.prototype.toJSONString; delete Ob ...

  5. 什么是物理像素、虚拟像素、逻辑像素、设备像素,什么又是 PPI, DPI, DPR 和 DIP

    什么是物理像素.虚拟像素.逻辑像素.设备像素,什么又是 PPI, DPI, DPR 和 DIP?有关 viewport 以及苹果安卓设备上的页面呈现为什么效果不一样,又有哪些方法去改变和统一呢?网络上 ...

  6. Oracle EBS 12.2.6 on VirtualBox

    Back in May, Oracle announced the general availability of Oracle VM Virtual Appliance for E-Business ...

  7. android_双击退出

    /** * 设置高速双击退出程序 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-g ...

  8. Windows Server 2012 R2 或 2016 无法安装 .NET Framework 3.5.1

    问题描述 使用 Windows Server 2012 R2 或 Windows Server 2016系统,发现在安装 .NET Framework 3.5.1 时报错,报错内容如下图所示. 原因分 ...

  9. linux 使用ifstat查看网络使用情况

    首先安装ifstat wget http://distfiles.macports.org/ifstat/ifstat-1.1.tar.gz tar xzvf ifstat-1.1.tar.gz cd ...

  10. mysql存储引擎的一点学习心得总结

    首先我们应该了解mysql中的一个重要特性--插件式存储引擎,从名字就能够看出在mysql中,用户能够依据自己的需求随意的选择存储引擎.实际上也是这样.即使在同一个数据库中.不同的表也能够使用不同的存 ...