读取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. 使用pickle模块存储对象

    import time import hashlib import pickle import os class Info(): def __init__(self): self.create_tim ...

  2. eclipse中配置server

    打开Eclipse,在打开上面的help--- install new software---- work with 里面点开选择--All Available Sites-- 等下面的pending ...

  3. Saltstack 安装 命令

    主机规划 10.0.0.21 master 10.0.0.22 minion 10.0.0.23 minion 基础环境 [root@10.0.0.21 ~]# cat /etc/redhat-rel ...

  4. 写日志(log)

    已下为我自己写的一个写日志的类,比较简洁. <?php class Log { /** * @Purpose : 写日志 * @Method Name : writeLog() * @param ...

  5. protobuf 测试使用

    1       介绍 Protocol buffers are a flexible, efficient, automated mechanism for serializing structure ...

  6. iOS 7 UI 过渡指南 - 開始之前(iOS 7 UI Transition Guide - Before You Start)

    iOS 7 UI Transition Guide Preparing for Transition Before You Start Scoping the Project Supporting i ...

  7. .Net AppDomain详解(二)

    AppDomain 类 表示应用程序域,它是一个应用程序在其中执行的独立环境. 此类不能被继承. 命名空间:   System程序集:  mscorlib(位于 mscorlib.dll) 继承层次结 ...

  8. Spark机器学习(3):保序回归算法

    保序回归即给定了一个无序的数字序列,通过修改其中元素的值,得到一个非递减的数字序列,要求是使得误差(预测值和实际值差的平方)最小.比如在动物身上实验某种药物,使用了不同的剂量,按理说剂量越大,有效的比 ...

  9. Spring MVC 中使用 Google kaptcha 验证码

    验证码是抵抗批量操作和恶意登录最有效的方式之一. 验证码从产生到现在已经衍生出了很多分支.方式.google kaptcha 是一个非常实用的验证码生成类库. 通过灵活的配置生成各种样式的验证码,并将 ...

  10. 基于Centos7.5搭建Docker环境

    docker很火,基于容器化技术,实现一次编译到运行.实现运行环境+服务的一键式打包! 00.部署环境 centos7.5(基于vmware搭建的测试环境,可以跟互联网交互,桥接方式联网) docke ...