依赖jar包:
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- <version>2.2.2</version>
- </dependency>
person.java
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:
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);
}
}
相关推荐
总的来说,Google Gson库在Android Studio中为开发者提供了一个强大且易于使用的工具,用于处理JSON数据。它能够有效地将JSON字符串转化为Java对象,以及将Java对象序列化为JSON字符串,极大地简化了数据操作的工作...
包含以下java源文件: ...com.google.gson.JsonNull.class com.google.gson.JsonObject.class com.google.gson.JsonParseException.class com.google.gson.JsonParser.class com.google.gson.JsonPrimitive.class ...
总结来说,无论是在Eclipse还是Android Studio,Google Gson库都是处理JSON数据的强大工具。通过简单易用的API,我们可以轻松地将JSON与Java对象相互转换,极大地提高了开发效率。在实际项目中,理解并熟练掌握Gson...
在本文中,我们将深入探讨如何使用Gson解析JSON数据,包括处理简单JSON和复杂的JSON结构。 首先,我们要了解JSON的基本概念。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,...
Google Gson库提供了一个强大的工具,使得Java对象与JSON字符串之间的转换变得简单易行。本文将详细介绍如何利用Google Gson库进行JSON字符串与对象之间的相互转换。 首先,我们需要了解Gson的核心概念。Gson库通过...
本篇文章将详细介绍如何使用Gson处理Json格式字符串,以及如何在实际项目中应用。 首先,我们需要了解JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成...
在实际应用中,使用Gson或FastJson可以简化JSON数据的处理,提高开发效率。例如,在Android应用程序中,可以使用Gson或FastJson将JSON数据解析成Java对象,或者将Java对象转换成JSON字符串。 使用Gson解析JSON的...
在Java开发中,处理JSON数据是一项常见的任务,Gson库是由Google提供的一个强大的工具,用于在Java对象和JSON数据之间进行映射。本篇文章将详细阐述如何使用Gson库解析JSON文件,帮助开发者理解并掌握这一技能。 ...
在这个场景中,我们将讨论如何在Android项目的`assets`目录下使用`Gson`来读取JSON文件,以及如何对JSON数据进行格式化操作。 首先,`assets`目录是Android项目中的一个特殊目录,用于存放应用运行时所需的静态资源...
在Android开发中,Gson库是一个非常常用的工具,它由Google提供,用于将Java对象转换为JSON字符串,以及将JSON数据转换回等效的Java对象。在这个场景中,"使用Gson解析json并且加载json中的网络图片"涉及到的关键...
总的来说,Google Gson提供了一个简单且高效的API,使得在Java(包括Android)项目中处理JSON数据变得非常便捷。通过学习和熟练掌握Gson,开发者能够更高效地实现数据的序列化和反序列化,从而提升开发效率和代码...
在Java开发中,Gson库是一个非常常用的工具,它由Google提供,用于将Java对象转换为JSON字符串,以及将JSON字符串反序列化回相应的Java对象。这个“Gson解析JSON数据的入门级源码”应该包含了一些基础示例,帮助初学...
5. **服务端代码**:在描述中提到的`MyeclipseService`可能是服务端提供的接口,它可能包含了接收请求、处理数据(如使用Gson解析或生成JSON)以及返回响应的功能。服务端通常会使用HTTP协议与客户端交互,通过...
本教程将深入探讨如何使用Gson库解析复杂的JSON实例,让你只需寥寥几行代码就能轻松处理JSON解析问题。 ### Gson库简介 Gson库的核心功能是将Java对象(如自定义类)转换为JSON字符串,反之亦然。这个库非常直观,...
这个“Google Gson解析Json数据应用实例”旨在深入探讨Gson库的使用,帮助开发者更好地理解和运用它来处理JSON数据。 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也...
GSON是Google提供的一个Java库,用于将Java对象转换为JSON格式的字符串,反之亦然。GSON的强大之处在于它可以自动映射JSON数据到Java对象,大大简化了JSON解析和序列化的过程。 以下是关于JSON和GSON在Android开发...
Gson是Google开发的一款强大的Java库,用于在Java对象和JSON数据之间进行映射。它的全称是Google Simple JSON,其版本2.3.1是一个较为稳定且广泛使用的版本,适用于许多Android和Java项目。本篇文章将深入探讨Gson ...
Gson是Google提供的一个开源库,全称为Google Simple JSON。它的主要功能是将Java对象转换为对应的JSON字符串,也可以将JSON数据反序列化为Java对象。Gson库的强大之处在于其灵活性和自动化处理,它能够自动映射JSON...
在Android开发中,Gson库是Google提供的一个强大的JSON数据解析工具,它使得Java对象与JSON数据之间的转换变得简单易行。本篇文章将详细介绍如何在Android应用中使用Gson库解析JSON数据,以便更好地理解这一关键知识...