算是第一次正式接触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(一)的更多相关文章

  1. GeoJson的生成与解析,JSON解析,Java读写geojson,geotools读取shp文件,Geotools中Geometry对象与GeoJson的相互转换

    GeoJson的生成与解析 一.wkt格式的geometry转成json格式 二.json格式转wkt格式 三.json格式的数据进行解析 四.Java读写geojson 五.geotools读取sh ...

  2. Java 的 JSON 开源类库选择比较(zz)

    在看了作者的介绍,然后我又到mvnrepository上去看了各个库的的使用数之后,发现只能在jackson和gson之间做选择. 以下是原文 有效选择七个关于Java的JSON开源类库 April  ...

  3. Android读写JSON格式的数据之JsonWriter和JsonReader

    近期的好几个月都没有搞Android编程了,逐渐的都忘却了一些东西.近期打算找一份Android的工作,要继续拾起曾经的东西.公司月初搬家之后就一直没有网络,直到今日公司才有网络接入,各部门才開始办公 ...

  4. Java之JSON操作(Jackson)

    Java to JSON: package json.jackson; import bean.User; import com.fasterxml.jackson.databind.ObjectMa ...

  5. Java解析json(二):jackson

    Java解析json(二):jackson   官方参考 Jackson Home Page:https://github.com/FasterXML/jackson Jackson Wiki:htt ...

  6. java读写大文件

    java读写2G以上的大文件(推荐使用以下方法) static String sourceFilePath = "H:\\DataSource-ready\\question.json&qu ...

  7. Java读写文本文件操作

    package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; ...

  8. java 读写word java 动态写入 模板文件

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import ja ...

  9. Java集合 Json集合之间的转换

    1. Java集合转换成Json集合 关键类:JSONArray jsonArray = JSONArray.fromObject(Object obj); 使用说明:将Java集合对象直接传进JSO ...

随机推荐

  1. 。Java注意事项

  2. 微信 xml 转 Map

    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; xml + ...

  3. python 列表常用操作

    例子: list1 = [1, 2, 3, 4, 5, 6, [6, 7, 8,9], 'hi', 'hello', 6] list2 = [7, 8, 1, 2] list3 = ['good', ...

  4. 关于F12的有效利用

    1.之前仅用F12查看页面的代码: 2.现在发现用F12可以查看css文件: 3.比如看到页面背景为黑色,然后在css里面把背景从黑色调成白色:

  5. 各版本Office办公软件下载

    各版本Office办公软件下载:http://pan.baidu.com/share/home?uk=1174874628

  6. iOS - OC NSStream 文件流

    前言 @interface NSStream : NSObject @interface NSOutputStream : NSStream 1.文件流的使用 NSString *filePath = ...

  7. FormData、Blob、File、ArrayBuffer数据类型

    XMLHttpRequest 1.0 1.0 中xmlhttpRequest有2个重要的返回属性,reposeText,reposeXml.下面的数据类型都是2.0新增的 FormData对象 我们应 ...

  8. Monkey 使用aapt查看apk包名

    使用aapt    //aapt是sdk自带的一个工具,在sdk\builds-tools\目录下1.以ES文件浏览器为例,命令行中切换到aapt.exe目录执行:aapt dump badging ...

  9. js正则表达式入门

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  10. MPICH3 配置安装问题列表

    问题列表 1: configure: error: F90 and F90FLAGS are replaced by FC and FCFLAGS respectively in this confi ...