• 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"}
  • 相关阅读:
    C# 字典 Dictionary 转 JSON 格式遍历
    jquery-easyui-tree异步树
    android 开发环境搭建
    解决android sdk 无法更新
    043——VUE中组件之使用.sync修饰符与computed计算属性实现购物车原理
    laravel中数据库迁移的使用:
    002PHP文件处理——文件处理 is_dir mkdir getcwd chdir rmdir
    001PHP文件处理——文件处理disk_total_space disk_free_space basename dirname file_exists filetype
    042——VUE中组件之子组件使用$on与$emit事件触发父组件实现购物车功能
    041——VUE中组件之pros数据的多种验证机制实例详解
  • 原文地址:https://www.cnblogs.com/not-NULL/p/5235378.html
Copyright © 2020-2023  润新知