java 读写JSON(一)
算是第一次正式接触Json,没有深入研究,先贴上java的代码,日后才说!
package priv.chenhy.datehandle; import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; /**
* @author
* @version
*
*/ /*
* 该方法需要依赖的包:json.lib+(ezmorph和commons的lang、logging、beanutils、collections)
*/
public class ReadWriteJson { @SuppressWarnings("finally")
private static List<JSONObject> ReadJsonFile(){
String path="./src/test.json";
File file=new File(path);
BufferedReader reader=null;
String jsonContent="";
List<JSONObject> jsonObjList=new ArrayList<JSONObject>(); try {
reader=new BufferedReader(new FileReader(file));
String tempString=null;
while((tempString=reader.readLine())!=null){
// System.out.println(tempString);
jsonContent+=tempString; //
JSONObject jo=JSONObject.fromObject(tempString);
jsonObjList.add(jo);
System.out.println("id:"+jo.get("_id"));
System.out.println("oid:"+jo.getJSONObject("_id").get("$oid"));
System.out.println("openid:"+jo.get("openid"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return jsonObjList;
} } private static void WriteJson() {
JSONObject obj = new JSONObject();
obj.put("name", "jack");
obj.put("age", new Integer(20));
obj.put("balance", new Double(12.34));
obj.put("is_virgin", new Boolean(true)); JSONObject obj1 = new JSONObject();
obj1.put("name", "jack");
obj1.put("age", new Integer(20));
obj1.put("balance", new Double(12.34));
obj1.put("is_virgin", new Boolean(true));
obj1.put("hehe", obj); System.out.println("obj1:" + obj1);
System.out.println("obj:" + obj);
} public static void main(String[] args) {
WriteJson();
ReadJsonFile();
}
}
代码中用到的json数据文件如下:
{"_id":{"$oid":"56cecb6c0755e10498842230"},"openid":"2252199","dev_id":"869654022644995","platform":1,"create_time":{"$date":"2016-02-25T09:37:48.139Z"},"channel":"yl","app_version":"0.1","pkg_sign":"1601970164"}
{"_id":{"$oid":"56cecb6d0755e10497842230"},"openid":"2687638","dev_id":"865168024215260","platform":1,"create_time":{"$date":"2016-02-25T09:37:49.080Z"},"channel":"yl","app_version":"0.1","pkg_sign":"1601970164"}
{"_id":{"$oid":"56cecb740755e10497842232"},"openid":"2527384","dev_id":"6EC47E5C-402A-4355-B6C7-4DFD2CA9A1B7","platform":2,"create_time":{"$date":"2016-02-25T09:37:56.796Z"},"channel":"yl","app_version":"1.0"}
{"_id":{"$oid":"56cecb770755e10499842231"},"openid":"2252199","dev_id":"869654022644995","platform":1,"create_time":{"$date":"2016-02-25T09:37:59.282Z"},"channel":"yl","app_version":"0.1","pkg_sign":"1601970164"}
{"_id":{"$oid":"56cecb770755e10497842233"},"openid":"2288891","dev_id":"866184027180988","platform":1,"create_time":{"$date":"2016-02-25T09:37:59.656Z"},"channel":"yl","app_version":"0.1","pkg_sign":"1601970164"}
{"_id":{"$oid":"56cecb780755e10499842232"},"openid":"3195291","dev_id":"867756021381099","platform":1,"create_time":{"$date":"2016-02-25T09:38:00.779Z"},"channel":"yl","app_version":"0.1","pkg_sign":"1601970164"}
{"_id":{"$oid":"56cecb7e0755e10497842235"},"openid":"2288891","dev_id":"866184027180988","platform":1,"create_time":{"$date":"2016-02-25T09:38:06.785Z"},"channel":"yl","app_version":"0.1","pkg_sign":"1601970164"}
{"_id":{"$oid":"56cecb870755e10499842235"},"openid":"2229493","dev_id":"6B38EE97-7B63-4BBD-9E39-69F9725D9E81","platform":2,"create_time":{"$date":"2016-02-25T09:38:15.393Z"},"channel":"yl","app_version":"1.0"}
{"_id":{"$oid":"56cecb8b0755e104c32a25fb"},"openid":"2717296","dev_id":"865308023993802","platform":1,"create_time":{"$date":"2016-02-25T09:38:19.046Z"},"channel":"yl","app_version":"0.1","pkg_sign":"1601970164"}
{"_id":{"$oid":"56cecb920755e104c32a25fc"},"openid":"3211441","dev_id":"574555438307688","platform":1,"create_time":{"$date":"2016-02-25T09:38:26.106Z"},"channel":"yl","app_version":"0.1","pkg_sign":"1601970164"}
java 读写JSON(一)的更多相关文章
- GeoJson的生成与解析,JSON解析,Java读写geojson,geotools读取shp文件,Geotools中Geometry对象与GeoJson的相互转换
GeoJson的生成与解析 一.wkt格式的geometry转成json格式 二.json格式转wkt格式 三.json格式的数据进行解析 四.Java读写geojson 五.geotools读取sh ...
- Java 的 JSON 开源类库选择比较(zz)
在看了作者的介绍,然后我又到mvnrepository上去看了各个库的的使用数之后,发现只能在jackson和gson之间做选择. 以下是原文 有效选择七个关于Java的JSON开源类库 April ...
- Android读写JSON格式的数据之JsonWriter和JsonReader
近期的好几个月都没有搞Android编程了,逐渐的都忘却了一些东西.近期打算找一份Android的工作,要继续拾起曾经的东西.公司月初搬家之后就一直没有网络,直到今日公司才有网络接入,各部门才開始办公 ...
- Java之JSON操作(Jackson)
Java to JSON: package json.jackson; import bean.User; import com.fasterxml.jackson.databind.ObjectMa ...
- Java解析json(二):jackson
Java解析json(二):jackson 官方参考 Jackson Home Page:https://github.com/FasterXML/jackson Jackson Wiki:htt ...
- java读写大文件
java读写2G以上的大文件(推荐使用以下方法) static String sourceFilePath = "H:\\DataSource-ready\\question.json&qu ...
- Java读写文本文件操作
package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; ...
- java 读写word java 动态写入 模板文件
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import ja ...
- Java集合 Json集合之间的转换
1. Java集合转换成Json集合 关键类:JSONArray jsonArray = JSONArray.fromObject(Object obj); 使用说明:将Java集合对象直接传进JSO ...
随机推荐
- ace_admin_1.3.1 wysiwyg 工具条下拉出不来
试了很久才知道是因为<script src="__PUBLIC__/assets/js/bootstrap.min.js"></script> 这个js加 ...
- 【Unity3D游戏开发】之游戏目录结构之最佳实践和优化 (十一)
游戏目录结构之最佳实践 前置条件 1.多人协作开发,git管理 2.游戏不大,所有Scene合并到一起Scene中,eg.RoleScene.MapScene.StoreScene 3.Master一 ...
- 阻抗计算(用SI9000如何计算微带线)
用SI9000如何计算微带线 一.几个概念: 阻抗的定义:在某一频率下,电子器件传输信号线中,相对某一参考层,其高频信号或电磁波在传播过程中所受的阻力称之为特性阻抗,它是 ...
- InfoObject共享
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- iOS 推送通知处理
//这是程序杀死后再通过点击通知进入时调用的方法 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti ...
- Android 实现闪屏页和右上角的倒计时跳转
效果图: 闪屏页用到了handler和CountDownTimer类,还需配置一下Activity的主题,这里是:android:theme="@android:style/Theme.No ...
- Spring事务管理只对出现运行期异常进行回滚
原文:http://blog.csdn.net/abc19900828/article/details/39497631 使用spring难免要用到spring的事务管理,要用事务管理又会很自然的选择 ...
- 转!!XML,DTD,XSD,XSL的区别
XML=可扩展标记语言(eXtensible Markup Language).可扩展标记语言XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可用 方便的方式建立,虽然XML占 ...
- [maven] 常用插件解析
参考资料:http://my.oschina.net/zh119893/blog/276090 我们都知道Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完 ...
- ssh下:系统初始化实现ServletContextListener接口时,获取spring中数据层对象无效的问题
想要实现的功能:SSH环境下,数据层都交由Spring管理:在服务启动时,将数据库中的一些数据加载到ServletContext中缓存起来. 系统初始化类需要实现两个接口: ServletContex ...