使用google的GSON处理JSON

依赖jar包:

  1. <dependency>  
  2.     <groupId>com.google.code.gson</groupId>  
  3.     <artifactId>gson</artifactId>  
  4.     <version>2.2.2</version>  
  5. </dependency>

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("D:/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);
    }
复制代码

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值