使用gson(一)
1、数组和json的转换
package com.test.gson;
import com.google.gson.Gson;
public class ArrayToJson { public static void main(String[] args) {
// TODO Auto-generated method stub
int[] numbers = {1,2,3,4,5,6,7,8,9};
String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; Gson gson = new Gson(); String numbersJson = gson.toJson(numbers);
System.out.println("numbersJson=" + numbersJson); int[] nums = gson.fromJson(numbersJson, int[].class);
for(int i=0; i<nums.length ; i++){
System.out.println(nums[i]);
}
String daysJson = gson.toJson(days);
System.out.println("daysJson=" + daysJson); String[] weekDays = gson.fromJson(daysJson, String[].class);
for(int j=0; j<weekDays.length; j++){
System.out.println(weekDays[j]);
}
} }
2、对象和json的转换
package com.test.gson; import com.google.gson.Gson; public class StudentToJson { public static void main(String[] args) {
// TODO Auto-generated method stub
Student student = new Student(1001,"gulu","beijing");
Gson gson = new Gson(); String studentJson = gson.toJson(student);
System.out.println("studentJson=" + studentJson); Student stu = gson.fromJson(studentJson, Student.class);
System.out.println(stu.toString());
System.out.println(stu.getId());
System.out.println(stu.getName());
System.out.println(stu.getAddress());
} }
3、List和json的转换
package com.test.gson; import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Type; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; public class CollectionToJson { public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> names = new ArrayList<String>();
names.add("Alice");
names.add("Bob");
names.add("Carol");
names.add("Mallory"); Gson gson = new Gson(); String jsonNames = gson.toJson(names);
System.out.println("jsonNames="+jsonNames); Student a = new Student(1,"zhangsan","beijing");
Student b = new Student(2,"wangwu","nanjing");
Student c = new Student(3,"lisi","guangzhou");
Student d = new Student(4,"wangba","shanghai"); List<Student> students = new ArrayList<Student>();
students.add(a);
students.add(b);
students.add(c);
students.add(d); String jsonStudents = gson.toJson(students);
System.out.println("jsonStudents="+jsonStudents); Type type = new TypeToken<List<Student>>(){}.getType();
List<Student> studentList = gson.fromJson(jsonStudents, type);
for(Student student : studentList){
System.out.println(student.getName());
}
} }
4、map和json的转换
package com.test.gson; import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; public class MapToJson { public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, String> colours = new HashMap<String,String>();
colours.put("black", "#000000");
colours.put("red", "#ff0000");
colours.put("green", "#00ff00");
colours.put("blue", "#0000ff"); Gson gson = new Gson();
String mapJson = gson.toJson(colours);
System.out.println("gson="+mapJson); Type type = new TypeToken<Map<String,String>>(){}.getType(); Map<String,String> map = gson.fromJson(mapJson, type);
for(String key : map.keySet()){
System.out.println("map.get=" + map.get(key));
}
} }
使用gson(一)的更多相关文章
- No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...
- Gson将字符串转换成JsonObject和JsonArray
以下均利用Gson来处理: 1.将bean转换成Json字符串: public static String beanToJSONString(Object bean) { return new Gso ...
- Gson解析纯Json数组
[ { "type": "123", "value": 123 }, { "type": "234" ...
- 【Gson】互相转化
Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. 对象转为字符串 Strin ...
- Android Gson解析
目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),解析速度最快的是Gson,下载地址:https://co ...
- Android总结之json解析(FastJson Gson 对比)
前言: 最近为了统一项目中使用的框架,发现项目中用到了两种json解析框架,他们就是当今非常主流的json解析框架:google的Gson 和阿里巴巴的FastJson,为了废除其中一个所以来个性能和 ...
- gson笔记 解析json数据
gson中负责json数据解析的类是JsonReader. Json格式有两种结构,一种是对象(键值对的组合,无序),另外一种是数组(值的有序集合). 因此针对这两种格式,JsonReader提供了不 ...
- Android Gson的使用总结
1.概念 Gson是谷歌发布的一个json解析框架 2.如何获取 github:https://github.com/google/gson android studio使用 compile 'com ...
- Android JSON、GSON、FastJson的封装与解析
声明: 1.本帖只提供代码,不深入讲解原理.如果读者想要深入了解,那就不要在这个帖子上浪费时间了 2.客户端用的是Google官方的Volley访问服务器,具体了解Volley请戳 这里 3.本帖三种 ...
- Java Gson 简要笔记
Gson是Google开发的Java比较好用的 Json工具. 使用挺简单,假设有个类: class Runner { int attr; String name; public Runner(int ...
随机推荐
- android 中文 api (71) —— BluetoothServerSocket[蓝牙]
前言 本章内容是 android.bluetooth.BluetoothServerSocket,为Android蓝牙部分的章节翻译.服务器通讯套接字,与TCP ServerSocket类似.版本为 ...
- 经典排序算法及python实现
今天我们来谈谈几种经典排序算法,然后用python来实现,最后通过数据来比较几个算法时间 选择排序 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理是每一次从待排序的数据 ...
- Ubuntu 15.04 Rails4.2.5 处理异常
1. 修改: /app/controllers/application_controller.rb文件为如下样子: class ApplicationController < ActionCon ...
- 「C」关键字、标识符、注释、内存分析、数据、常量、变量
一.关键字 C语言提供的有特殊含义的符号,共32个. 一般在Xcode中关键字全部显示紫褐色,关键字全部都为小写.如int.return等. 二.标识符 标识符是程序员在程序中自定义的一些符号和名称. ...
- Zend Studio 如何配置本地apache服务器使用xdebug调试php脚本
本地环境搭配: apache 2.2 安装位置:D:/program files/Apache Software Foundation/Apache2.2 php 5.2.10 安装位置:C:/php ...
- (IOS)数据持久化
1.属性列表序列化 2.模型对象归档 3.嵌入式SQLite3 4.Core Data 5.应用程序设置 6.UIDocument管理文档存储 7.iCloud Demo界面: 1.属性列表序列化 即 ...
- asp.net mvc 客户端(&)中检测到有潜在危险的 Request.Path 值。
出现这个错误后,试过 <pages validateRequest="false"> <httpRuntime requestValidationMode=&qu ...
- [转] HTC:html组件
~~~不时会看到 用htc实现 hover, border-raius等效果,可以用来弥补IE6-8的不足.那么htc到底是什么呢? 1.摘要 本文在实例的基础上讨论了HTC(HTML Compone ...
- referer htttp headers 统计信息 防盗链
HTTP headers是HTTP请求和相应的核心模块,它承载了关于客户端浏览器.请求页面.服务器等相关信息.Referer是HTTP头中的一个属性,告诉服务器我是从哪个页面链接过来的,所携带的信息用 ...
- 在 Windows Azure 网站 (WAWS) 上对 Orchard CMS 使用 Azure 缓存
编辑人员注释: 本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 撰写. 如果您当前的 OrchardCMS 网站在 Windows Azure 网站 ...