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"}

 

posted on 2016-03-02 16:11  是知也  阅读(4121)  评论(0编辑  收藏  举报

导航