现在的位置: 首页 > 综合 > 正文

使用google的GSON处理JSON

2012年10月18日 ⁄ 综合 ⁄ 共 12812字 ⁄ 字号 评论关闭

/Files/jxgxy/testJSON.rar 

 

person.java

package com.eboy.testJSON;

public class person {
    private String url_3w;
    private int replyCount;
    private int hasImg;
    private String digest;
    private String url;
    private String docid;
    private String title;
    private int order;
    private int priority;
    private String lmodify; //有可能是日期类型
    private String subtitle;
    private String imgsrc;
    private String ptime; //有可能是日期类型
    public String getUrl_3w() {
        return url_3w;
    }
    public void setUrl_3w(String url_3w) {
        this.url_3w = url_3w;
    }
    public int getReplyCount() {
        return replyCount;
    }
    public void setReplyCount(int replyCount) {
        this.replyCount = replyCount;
    }
    public int getHasImg() {
        return hasImg;
    }
    public void setHasImg(int hasImg) {
        this.hasImg = hasImg;
    }
    public String getDigest() {
        return digest;
    }
    public void setDigest(String digest) {
        this.digest = digest;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getDocid() {
        return docid;
    }
    public void setDocid(String docid) {
        this.docid = docid;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public int getOrder() {
        return order;
    }
    public void setOrder(int order) {
        this.order = order;
    }
    public int getPriority() {
        return priority;
    }
    public void setPriority(int priority) {
        this.priority = priority;
    }
    public String getLmodify() {
        return lmodify;
    }
    public void setLmodify(String lmodify) {
        this.lmodify = lmodify;
    }
    public String getSubtitle() {
        return subtitle;
    }
    public void setSubtitle(String subtitle) {
        this.subtitle = subtitle;
    }
    public String getImgsrc() {
        return imgsrc;
    }
    public void setImgsrc(String imgsrc) {
        this.imgsrc = imgsrc;
    }
    public String getPtime() {
        return ptime;
    }
    public void setPtime(String ptime) {
        this.ptime = ptime;
    }        
}

 

163.txt

[{"url_3w":"http://tech.163.com/12/0802/18/87U2O5HA000915BE.html","replyCount":42,"hasImg":1,"digest":"寻求小区通信垄断;据称贿赂已成当地潜规则。","url":"http://3g.163.com/tech/12/0802/18/87U2O5HA000915BE.html","docid":"87U2O5HA000915BE","title":"联通贿赂房企求垄断遭罚20万","order":1,"priority":79,"lmodify":"2012-08-02 18:33:41","subtitle":"已成当地“潜规则”","imgsrc":"http://img5.cache.netease.com/3g/2012/8/2/201208021859373be50.jpg","ptime":"2012-08-02 18:33:41"},{"url_3w":"http://tech.163.com/12/0802/15/87TP763H000915BD.html","docid":"87TP763H000915BD","title":"索尼第一财季净亏3.14亿美元","replyCount":6,"priority":79,"lmodify":"2012-08-02 20:55:23","imgsrc":"http://img2.cache.netease.com/3g/2012/8/2/20120802203033fc8ed.jpg","subtitle":"","digest":"因重组成本加大;运营利润预期下调。   ","ptime":"2012-08-02 15:47:08","url":"http://3g.163.com/tech/12/0802/15/87TP763H000915BD.html"},{"url_3w":"http://tech.163.com/12/0802/01/87S6O8UK000915BD.html","docid":"87S6O8UK000915BD","title":"微软宣布Windows8开发完成","replyCount":375,"priority":79,"lmodify":"2012-08-02 01:52:04","imgsrc":"http://img3.cache.netease.com/3g/2012/7/14/201207140905041b3f8.jpg","subtitle":"","digest":"将交付RTM版本;将于10月26日正式发售。","ptime":"2012-08-02 01:05:10","url":"http://3g.163.com/tech/12/0802/01/87S6O8UK000915BD.html"},{"url_3w":"http://tech.163.com/12/0802/17/87TV0VVE000915BE.html","docid":"87TV0VVE000915BE","title":"Android 4.1使用率达0.8%","replyCount":0,"priority":78,"lmodify":"2012-08-02 20:55:40","imgsrc":"http://img4.cache.netease.com/3g/2012/8/2/20120802204956e3326.jpg","subtitle":"系统版本分裂问题仍在","digest":"其正式推出仅三个星期;系统版本分裂问题仍在。","ptime":"2012-08-02 17:28:36","url":"http://3g.163.com/tech/12/0802/17/87TV0VVE000915BE.html"},{"url_3w":"http://mobile.163.com/12/0802/10/87T82JT7001130A0.html","docid":"87T82JT7001130A0","title":"普通用户三大途径获取Win 8","replyCount":88,"priority":78,"lmodify":"2012-08-02 11:08:25","imgsrc":"http://img3.cache.netease.com/3g/2012/8/2/201208021110144ff05.jpg","subtitle":"","digest":"如通过升级优惠计划,98元即可获取。  ","ptime":"2012-08-02 10:47:31","url":"http://3g.163.com/mobile/12/0802/10/87T82JT7001130A0.html"},{"url_3w":"http://mobile.163.com/12/0802/11/87TA7KQK0011665S.html","docid":"87TA7KQK0011665S","title":"IDG:苹果占全球手机75%利润","replyCount":55,"priority":77,"lmodify":"2012-08-02 13:56:33","imgsrc":"http://img2.cache.netease.com/3g/2012/8/2/201208021357545ee8e.jpg","subtitle":"","digest":"而出货量只占9%;平板电脑居苹果统治地位。","ptime":"2012-08-02 11:19:17","url":"http://3g.163.com/mobile/12/0802/11/87TA7KQK0011665S.html"},{"url_3w":"http://tech.163.com/12/0802/06/87SOM2VL000915BE.html","docid":"87SOM2VL000915BE","title":"设计师回顾iPhone诞生过程","replyCount":192,"priority":77,"lmodify":"2012-08-02 08:21:46","imgsrc":"http://img1.cache.netease.com/3g/2012/8/2/20120802083117d8d9e.jpg","subtitle":"","digest":"苹果和三星专利诉讼案开庭,苹果设计团队露面。","ptime":"2012-08-02 06:18:33","url":"http://3g.163.com/tech/12/0802/06/87SOM2VL000915BE.html"},{"url_3w":"http://help.3g.163.com/12/0802/13/87THP14I00963VRO.html","docid":"87THP14I00963VRO","title":"研究称熬夜看电脑易患抑郁","replyCount":21,"priority":76,"lmodify":"2012-08-02 13:37:24","imgsrc":"http://img1.cache.netease.com/3g/2012/8/2/20120802133902f5a22.jpg","subtitle":"","digest":"人造光源增多与抑郁症发病率可能有一定关联。","ptime":"2012-08-02 13:37:04","url":"http://3g.163.com/ntes/12/0802/13/87THP14I00963VRO.html"},{"url_3w":"http://mobile.163.com/12/0802/11/87TA7KQL0011665S.html","docid":"87TA7KQL0011665S","title":"买家给差评接400个骚扰电话","replyCount":112,"priority":76,"lmodify":"2012-08-02 13:44:27","imgsrc":"http://img1.cache.netease.com/3g/2012/8/2/201208021347434d63c.jpg","subtitle":"","digest":"店主否认骚扰,并声称记者“搞错了”。  ","ptime":"2012-08-02 11:22:52","url":"http://3g.163.com/mobile/12/0802/11/87TA7KQL0011665S.html"},{"url_3w":"http://mobile.163.com/12/0802/10/87T5SO0P0011665S.html","docid":"87T5SO0P0011665S","title":"美吁上班勿看奥运防网络瘫痪","replyCount":40,"priority":76,"lmodify":"2012-08-02 11:43:43","imgsrc":"http://img6.cache.netease.com/3g/2012/8/2/20120802114537c5d3b.jpg","subtitle":"","digest":"报道称电邮发出当日美国网速慢如蜗牛。  ","ptime":"2012-08-02 10:00:37","url":"http://3g.163.com/mobile/12/0802/10/87T5SO0P0011665S.html"},{"url_3w":"http://mobile.163.com/12/0802/10/87T8D0QE0011665S.html","docid":"87T8D0QE0011665S","title":"上帝粒子:不存在几率3亿分之1","replyCount":70,"priority":75,"lmodify":"2012-08-02 11:25:57","imgsrc":"http://img4.cache.netease.com/3g/2012/7/9/20120709102058fc998.jpg","subtitle":"","digest":"只要误差少于350万分之一,便可发现一种粒子。","ptime":"2012-08-02 10:45:15","url":"http://3g.163.com/mobile/12/0802/10/87T8D0QE0011665S.html"},{"url_3w":"http://mobile.163.com/12/0802/10/87T7T46F0011665S.html","docid":"87T7T46F0011665S","title":"记者体验:天猫赔付没那么容易","replyCount":136,"priority":75,"lmodify":"2012-08-02 11:19:07","imgsrc":"http://img3.cache.netease.com/3g/2012/6/29/20120629081527446d8.jpg","subtitle":"","digest":"天猫“延迟发货,缺货赔付”制度效力有限。","ptime":"2012-08-02 10:34:31","url":"http://3g.163.com/mobile/12/0802/10/87T7T46F0011665S.html"},{"url_3w":"http://tech.163.com/12/0802/10/87T5ECQ8000915BE.html","docid":"87T5ECQ8000915BE","title":"诺基亚将关闭澳大利亚Qt部门","replyCount":13,"priority":75,"lmodify":"2012-08-02 11:00:26","imgsrc":"http://img6.cache.netease.com/3g/2012/8/2/2012080211013965d5f.jpg","subtitle":"","digest":"08年收购该项目;裁员人数不详。    ","ptime":"2012-08-02 10:01:32","url":"http://3g.163.com/tech/12/0802/10/87T5ECQ8000915BE.html"},{"url_3w":"http://mobile.163.com/12/0802/10/87T5SO0R0011665S.html","docid":"87T5SO0R0011665S","title":"美国iPhone用户正赶超安卓","replyCount":60,"priority":75,"lmodify":"2012-08-02 11:48:23","imgsrc":"http://img5.cache.netease.com/3g/2012/8/2/20120802133007e587e.jpg","subtitle":"","digest":"截至第二季度末,二者差距缩短至9个百分点。","ptime":"2012-08-02 09:58:20","url":"http://3g.163.com/mobile/12/0802/10/87T5SO0R0011665S.html"},{"url_3w":"http://tech.163.com/12/0801/00/87PGL9UQ000915BF.html","docid":"87PGL9UQ000915BF","title":"网易新闻APP成移动奥运首选","replyCount":164,"priority":75,"lmodify":"2012-08-01 14:34:39","imgsrc":"http://img3.cache.netease.com/3g/2012/8/1/20120801091018c0382.jpg","subtitle":"","digest":"第三方最新数据显示,关注频率优于其他同类产品。","ptime":"2012-08-01 00:00:36","TAG":"视频","url":"http://3g.163.com/tech/12/0801/00/87PGL9UQ000915BF.html"},{"url_3w":"http://tech.163.com/12/0802/10/87T746JB000915BD.html","docid":"87T746JB000915BD","title":"松下总部职员缩减至150人","replyCount":30,"priority":74,"lmodify":"2012-08-02 11:35:23","imgsrc":"http://img5.cache.netease.com/3g/2012/5/29/20120529103334f5751.jpg","subtitle":"大部分人被转至支援部门","digest":"大部分员工将被转移至新设立的支援部门。 ","ptime":"2012-08-02 10:30:56","url":"http://3g.163.com/tech/12/0802/10/87T746JB000915BD.html"},{"url_3w":"http://tech.163.com/12/0802/08/87T0U819000915BE.html","docid":"87T0U819000915BE","title":"长城宽带4兆测速仅为48K","replyCount":529,"priority":74,"lmodify":"2012-08-02 10:02:12","imgsrc":"http://img4.cache.netease.com/3g/2012/8/2/20120802101107514eb.jpg","subtitle":"","digest":"涉嫌消费欺诈;工作人员一问三不知。   ","ptime":"2012-08-02 08:42:49","url":"http://3g.163.com/tech/12/0802/08/87T0U819000915BE.html"},{"url_3w":"http://tech.163.com/12/0802/07/87SSQ6AN00094MOK.html","docid":"87SSQ6AN00094MOK","title":"美9万无线宽带用户转投有线","replyCount":6,"priority":74,"lmodify":"2012-08-02 10:40:37","imgsrc":"http://img2.cache.netease.com/3g/2012/8/2/201208021042586a7f5.jpg","subtitle":"","digest":"电话公司需推出更快更好的服务争夺用户。 ","ptime":"2012-08-02 07:18:00","url":"http://3g.163.com/tech/12/0802/07/87SSQ6AN00094MOK.html"},{"url_3w":"http://digi.163.com/12/0802/06/87SOE0HC001618J1.html","docid":"87SOE0HC001618J1","title":"国外玩家造街机版《暗黑3》","replyCount":22,"priority":74,"lmodify":"2012-08-02 09:49:09","imgsrc":"http://img6.cache.netease.com/3g/2012/8/2/20120802092253ef868.jpg","subtitle":"","digest":"通过街机的摇杆代替鼠标控制和角色移动。 ","ptime":"2012-08-02 06:14:05","url":"http://3g.163.com/digi/12/0802/06/87SOE0HC001618J1.html"},{"url_3w":"http://tech.163.com/12/0802/03/87SDFEPI00094MOK.html","docid":"87SDFEPI00094MOK","title":"小米遭电视求职者起诉违约","replyCount":90,"priority":74,"lmodify":"2012-08-02 10:25:12","imgsrc":"http://img1.cache.netease.com/3g/2012/8/2/2012080210233571dfb.jpg","subtitle":"","digest":"求职者电视应聘成功,报到时面试却被拒。 ","ptime":"2012-08-02 03:02:22","url":"http://3g.163.com/tech/12/0802/03/87SDFEPI00094MOK.html"}] 

 

Json.java:

package com.eboy.testJSON;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class testJSON {

    /**
     * 
@param args
     
*/
    public static void main(String[] args) {
        PersonToJson(); //person对象转为json字符串
        System.out.println("-----------------------------------");
        JsonToPerson("163.txt"); //将json字符串解析为person对象
    }

    private static void JsonToPerson(String string) {
        Gson gson = new Gson();
        try{
            String str = ReadTxtFile("163.txt");
            //System.out.println(str1);
            List<person> ps1 = gson.fromJson(str, new TypeToken<List<person>>(){}.getType());
            for (person p : ps1){
                System.out.println("Digest: " + p.getDigest());
                System.out.println("title: " + p.getTitle());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void PersonToJson() {
        Gson gson = new Gson();
        List<person> ps = new ArrayList<person>();
        for (int i=0; i<10; i++){
            person p = new person();
            p.setUrl_3w("http://tech.163.com/12/0802/18/87U2O5HA000915BE.html");
            p.setReplyCount(42);
            p.setHasImg(1);
            p.setDigest("寻求小区通信垄断;据称贿赂已成当地潜规则。" + i);
            p.setUrl("http://3g.163.com/tech/12/0802/18/87U2O5HA000915BE.html");
            p.setDocid("87U2O5HA000915BE");
            p.setTitle("联通贿赂房企求垄断遭罚20万");
            p.setOrder(1);
            p.setPriority(79);
            p.setLmodify("2012-08-02 18:33:41");
            p.setSubtitle("已成当地“潜规则”");
            p.setImgsrc("http://img5.cache.netease.com/3g/2012/8/2/201208021859373be50.jpg");
            p.setPtime("2012-08-02 18:33:41");
            ps.add(p);
        }
        String str = gson.toJson(ps);        
        System.out.println(str);        
    }

    private static String ReadTxtFile(String FileName) throws Exception {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(FileName));
        ByteArrayOutputStream memStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = bufferedInputStream.read(buffer)) != -1){
            memStream.write(buffer, 0, len);
        }
        byte[] data = memStream.toByteArray();        
        bufferedInputStream.close();
        memStream.close();
        bufferedInputStream.close();
        return new String(data);
    }

}

抱歉!评论已关闭.