JSON初体验(二):Gson解析
今天,我们来介绍一下Gson的jar包的用法.
JSON解析之Gson
特点:编码简介,谷歌官方推荐
数据之间的转换:
1.将json格式的字符串{}转换成为java对象
API:
<T> T fromJson(String json,Class<T> classOfT);
注意:要求json对象中的key的名称与java对象对应的类中的属性名要相同
步骤:
1.将Gson的jar包导入到项目中
2.创建Gson对象:
Gson gson = new Gson();
3.通过创建的Gson对象调用fromJson()方法,返回该JSON数据对应的java对象:
ShopInfo shopInfo = gson.fromJson(json,ShopInfo.class);
代码:
a.gson中所对应的类:
public class ShopInfo {
private int id;
private String name;
private double price;
private String imagePath; public ShopInfo() {
} public ShopInfo(int id, String name, double price, String imagePath) {
this.id = id;
this.name = name;
this.price = price;
this.imagePath = imagePath;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public double getPrice() {
return price;
} public void setPrice(double price) {
this.price = price;
} public String getImagePath() {
return imagePath;
} public void setImagePath(String imagePath) {
this.imagePath = imagePath;
} @Override
public String toString() {
return "ShopInfo{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", imagePath='" + imagePath + '\'' +
'}';
}
}
b.解析json方法:
public class One {
public static void main(String[] args) {
String json = "{\n" +
"\"id\":2,\n" +
"\"name\":\"大虾\",\n" +
"\"price\":23.3,\n" +
"\"imagePath\":\"http://www.baidu.com\"\n" +
"}";
Gson gson = new Gson();
ShopInfo shopInfo = gson.fromJson(json, ShopInfo.class);
System.out.println(shopInfo.toString());
}
}
2.将json格式的字符串[]转化为java对象的List
API:
fromJson(String json,Type typeOfT);
步骤:
1.将Gson的jar包导入到项目中
2.创建Gson对象:Gson gson = new Gson();
3.通过创建的Gson对象调用fromJson()方法,返回该JSON数据对应的java集合:
List<ShopInfo> shops = gson.fromJson(json,new TypeToken<List<ShopInfo>>(){}.getType());
代码:
public class Two {
public static void main(String[] args) {
String json = "[\n" +
" {\n" +
" \"id\":1,\n" +
" \"imagePath\":\"http://www.baidu.com\",\n" +
" \"name\":\"大虾1\",\n" +
" \"price\":12.3\n" +
" },\n" +
" {\n" +
" \"id\":2,\n" +
" \"imagePath\":\"http://www.douban.com\",\n" +
" \"name\":\"大虾2\",\n" +
" \"price\":12.5\n" +
" }\n" +
"]";
Gson gson = new Gson();
List<ShopInfo> shops = gson.fromJson(json, new TypeToken<List<ShopInfo>>() {
}.getType());
for (int i = 0; i <shops.size() ; i++) {
System.out.println(shops.toString());
}
}
}
3.将java对象转换为json字符串{}
API:
String toJson(Object src);
步骤:
1.将Gson的jar包导入到项目中
2.创建Gson对象:
Gson gson = new Gson();
3.通过创建的Gson对象调用toJson()方法,返回json数据;
ShopInfo shop = new ShopInfo(1,"鲍鱼",250.0,"");
String json = gson.toJson(shop);
代码:
public class Three {
public static void main(String[] args) {
Gson gson = new Gson();
ShopInfo shopInfo = new ShopInfo(1, "鲍鱼", 250.0, "");
String json = gson.toJson(shopInfo);
System.out.println(json);
}
}
4.将java对象的List转换为json字符串[]
API:
String toJson(Object src);
步骤:
1.将Gson的jar包导入到项目中
2.创建Gson对象:
Gson gson = new Gson();
3.通过创建的Gson对象调用toJson()方法,返回json数据:
List<ShopInfo> shops = new ArrayList<>();
String json = gson.toJson(shops);
public class Four {
public static void main(String[] args) {
ArrayList<ShopInfo> shopInfos = new ArrayList<ShopInfo>();
ShopInfo shopInfo1 = new ShopInfo(1, "鲍鱼1", 250.0, "");
ShopInfo shopInfo2 = new ShopInfo(2, "鲍鱼2", 250.2, "");
shopInfos.add(shopInfo1);
shopInfos.add(shopInfo2);
Gson gson = new Gson();
String json = gson.toJson(shopInfos);
System.out.println(json);
}
}
最后在贴一下gson的maven的依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
JSON初体验(二):Gson解析的更多相关文章
- JSON初体验(三):FastJson解析
JSON解析之FastJson(阿里巴巴解析开源) 特点: Fastjson是一个Java语言编写的高性能功能完善的JSON库,它采用的 是一种"假定有序快速匹配"的算法,把JSO ...
- JSON初体验(一):JsonObject解析
在学校的呆了一段时间,马上又要回去工作了,不说了,我现在介绍一下json相关的内容 1.JSON数据格式(总的来说,json就是一个字符串) 1.整体结构 String json1 = "{ ...
- SpringBoot初体验及原理解析
一.前言 上篇文章,我们聊到了SpringBoot得以实现的幕后推手,这次我们来用SpringBoot开始HelloWorld之旅.SpringBoot是Spring框架对“约定大于配置(Conv ...
- Ruby on rails初体验(二)
体验一中添加了一个最基本的支架和一个简单的数据迁移,实现了一个基本的增删改查的功能列表.体验二中要在次功能上继续丰满一下功能.实现如下效果: 在每个公司中都包含有不同的部门,按照体验一中的方法,添加一 ...
- 【原创】Jquery初体验二
快速导航 一.传统方式生成Table 二.使用jquery.tmpl插件快速生成Table 三.Jquery中的操作class的几个方法 四:jq里面的克隆 五:属性过滤器 六:表单元素过滤器 一.传 ...
- Spring Cloud Alibaba 初体验(二) Nacos 服务注册与发现 + 集成 Spring Cloud Gateway
一.服务注册 添加依赖: <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>s ...
- node初体验(二)
1.静态资源访问,需要设置路由和响应标头 2.url模块.path模块.querystring模块 Url { protocol: null, slashes: null, auth: null, h ...
- jquery.fn.extend与jquery.extend--(初体验二)
1.jquery.extend(object); 为扩展jQuery类本身.为类添加新的方法. jquery.fn.extend(object);给jQuery对象添加方法. $.extend({ a ...
- Google Gson解析Json数据应用实例
转自:http://lixigao449778967.blog.163.com/blog/static/24985164201269105928783/ 1.需要的Jar包 1) Google Gso ...
随机推荐
- CSS z-index的用法
理清 position及z-index的用法: static : 无特殊定位,对象遵循HTML定位规则absolute : 将对象从文档流中拖出,使用left,right,top,bottom等属 ...
- JS入口函数和JQuery入口函数
首先,讲一下它们的区别: (1)JS的window.onload事件必须要等到所有内容,以及外部图片之类的文件加载完之后,才会去执行. (2)JQuery入口函数是在所有标签加载完之后,就会去执行. ...
- June 13th 2017 Week 24th Tuesday
There are no regrets in life, just lessons. 人生中没有后悔,只有教训. Some people can learn from their past mist ...
- Python3基本数据类型(三、列表)
序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字-它的位置,或索引,第一个索引是0,第二个索引是1,以此类推.Python有6个序列的内置类型,但最常见的是列表和元组.序列都可以进 ...
- 最简单的docker教程:在docker里运行nginx服务器
命令行docker search nginx搜索名为nginx的docker image,返回结果的第一个,github上有10293个star,这就是我们想要搜索的结果: 使用命令docker pu ...
- MovieReview—Ghost in the shell(攻壳机动队95版)
About Future And is she really human? She’s just so something new A waking lithium flower ...
- Andriod ADB Interface驱动安装失败Configure USB Debug for Android
介绍: Linux或Apple或OS X ,已经安装了USB驱动调试为Android的帮助,确认您的Android USB调试连接配置和正常工作. Windows下需要自己手动下载驱动安装或者通过下载 ...
- A potentially dangerous Request.Form value was detected from the client的解决办法
网上找了这么多,这条最靠谱,记录下来,以备后用 <httpRuntime requestValidationMode="2.0"/> <pages validat ...
- 【luogu P3375 KMP字符串匹配】 模板
题目链接:https://www.luogu.org/problemnew/show/P3375 精华:在每次失配后不从头匹配而是尝试找一个新的开始并且是新开始的位置最长的相同前缀和后缀. 实际上KM ...
- 【luogu P2251 质量检测】 题解
也是一道ST表的板子题,很裸的RMQ问题,只需要在查询区间时处理一下下就好啦~ #include <cstdio> #include <algorithm> using nam ...