java 读写JSON(一)
2016-03-02 16:11
351 查看
算是第一次正式接触Json,没有深入研究,先贴上java的代码,日后才说!
代码中用到的json数据文件如下:
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小游戏第四弹 五子棋
- spring-cloud-bus
- Hibernate在Spring工程上的配置详解
- 【Android注释技巧】Android函数上面的注释你是怎么写的?(Eclipse中)
- ZigZag Conversion
- JAVA学习笔记 JDK下载、安装与测试
- Java 机器学习 工具 & 库
- 搜狗2015校园招聘javaproject师面经
- java判断字符串中是否包含字母字符
- [疯狂Java笔记]AWT:BoxLayout盒式布局、Box容器
- JAVA 5.17习题
- SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-002-激活PROFILE、设置默认值、@ActiveProfiles
- Java排序算法总结之(三)——选择排序(简单选择排序、堆排序)
- 深入理解java动态代理机制
- Spring工作原理
- SpringMVC系列之基本配置
- Java内部类总结
- java生成二维码到页面、到本地
- Java中的main()方法详解
- Spring MVC 解读——@Autowired、@Controller、@Service从原理层面来分析