Groovy 读取json文件,并用gson反序列化为List集合
Groovy 读取json文件,并用gson反序列化
package com.bicycle.util
import bicycle_grails.StationInfo
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.google.gson.reflect.TypeToken
import java.io.*
/**
* Created by gao on 2017/3/22.
*/
class Test {
static void main(String[] args) {
String data = ReadFile("C:\\project\\station.json");
SqlConnect.init()
//创建gson对象,用于json处理
Gson gson=new Gson()
//将从json字符串中读取的数据转换为接送对象
JsonObject returnData = new JsonParser().parse(data).getAsJsonObject()
//获取json数组对象
JsonArray jsonArray= returnData.getAsJsonArray("station")
//将json数组对象转换成list集合,gson提供fromJson方法进行放序列化
List<StationInfo> retList = gson.fromJson(jsonArray, new TypeToken<List<StationInfo>>(){}.getType())
for(StationInfo stationInfo:retList){
def sql="insert into station_info (station_id, area_id,station_name,lng,lat,lng1,lat1,stall_num,address,is_all_day,person_duty) values (${stationInfo.station_id}, ${stationInfo.area_id},${stationInfo.station_name},${stationInfo.lng},${stationInfo.lat},${stationInfo.lng1},${stationInfo.lat1},${stationInfo.stall_num},${stationInfo.address},${stationInfo.is_all_day},${stationInfo.person_duty})"
println(sql)
SqlConnect.getConnection().execute(sql)
}
}
static String ReadFile(String Path) {
BufferedReader reader = null
String laststr = ""
try {
//将json文件读入fileInputStream对象
FileInputStream fileInputStream = new FileInputStream(Path)
//将文件流转换为字符输入流,格式为UTF-8
//InputStreamReader : 是字节流与字符流之间的桥梁,能将字节流输出为字符流,并且能为字节流指定字符集,可输出一个个的字符;
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8")
//BufferedReader : 提供通用的缓冲方式文本读取,readLine读取一个文本行,也可以从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。
reader = new BufferedReader(inputStreamReader)
String tempString = null
while ((tempString = reader.readLine()) != null) {
laststr += tempString
}
reader.close()
} catch (IOException e) {
e.printStackTrace()
} finally {
if (reader != null) {
try {
reader.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
return laststr
}
}
Groovy 读取json文件,并用gson反序列化为List集合的更多相关文章
- cocos2d-x 读取 json 文件并用 jsoncpp 做解析
一码胜万言(请看注释) CclUtil.h // // CclUtil.h // PracticeDemo // // Created by kodeyang on 8/1/13. // // #if ...
- Unity的Json解析<一>--读取Json文件
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50373558 作者:car ...
- Asp.Net MVC 读取json文件
有些系统上面的配置可以做成config里面的appsetting.这里要求写在json文件里面. 首先 添加命名空间 using Newtonsoft.Json; using System.IO; u ...
- jquery无法读取json文件问题
jquery无法读取json文件,如:user.json文件无法读取.把user.json文件的后缀名修改为aspx,文件内容不变,则可以读取~ 原理不懂!~~
- Unity 用C#脚本读取JSON文件数据
读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON ...
- jQuery读取json文件,实现省市区/县(国标)三级联动
最近做一个微信项目,需要用户填写所在的省市区/县,决定使用jQuery读取json文件来实现省市区/县的联动. 其实很简单,jQuery文档也有详细解释: 代码如下: html <table w ...
- jqery ajax读取json文件
json文件数据 [ {"name":"哈哈··","email":"邮箱01","gender": ...
- spring注解读取json文件
开发时候在接口没有提供的时候,可以用json文件提前模拟接口数据 1.service层 package com.syp.spring.service; import java.io.File; imp ...
- Android - 读取JSON文件数据
Android读取JSON文件数据 JSON - JavaScript Object Notation 是一种存储和交换文本信息的语法. JSON对象在花括号中书写.用逗号来分隔值. JSON数组在方 ...
随机推荐
- 【整理总结】目录 - 代码沉淀 - 常见Nuget包介绍及使用
为了团队项目或者后期代码维护方便,现在项目已经极致推荐使用Nuget管理第三方类库了,所以下面列举的类库,都会在Nuget上获取到.目录排序不分先后,如果查找困难,请使用 ctrl + F 重要提示: ...
- [html][javascript]动态增删页面元素
<script type="text/javascript"> function append(event){ var myhref = document.create ...
- 030:Cetus中间件和MHA读写分离
030:Cetus中间件和MHA读写分离 line:V1.1 mail: gczheng@139.com date: 2018-08-30 一.主机环境 虚拟机配置 CPU 内存 硬盘 OS版本 My ...
- MySql 日期函数比较
查询当天数据 select * from tab where FROM_UNIXTIME(fabutime, '%Y%m%d') = 20121217; mysql TO_DAYS(date) 函 ...
- WCF传输大数据 --断点续传(upload、download)
using System; using System.IO; using System.Runtime.Serialization; using System.ServiceModel; namesp ...
- dd命令的conv=fsync,oflag=sync/dsync
conv的参数有 1.sync Pad every input block to size of 'ibs' with trailing zero bytes. When used with 'blo ...
- How to run eclipse in clean mode? and what happens if we do so?
What it does: if set to "true", any cached data used by the OSGi framework and eclipse run ...
- 问题记录,StartCoroutine(“str")问题
StartCoroutine参数为函数字符串名,运行时出错,错误是:无法启动协程函数. 调用格式如下: gameManager.StartCoroutine(LuaOnLevelwasloaded() ...
- ajax基本常识及get请求方式
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- ubuntu ibus&language 启动失败
[ubuntu ibus&language 启动失败] 版本:ubuntu 10.04 现像:language support & ibus 无法启动,导致无法使用中文输入法 原因:l ...