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(一)的更多相关文章

  1. 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 ...

  2. Gson将字符串转换成JsonObject和JsonArray

    以下均利用Gson来处理: 1.将bean转换成Json字符串: public static String beanToJSONString(Object bean) { return new Gso ...

  3. Gson解析纯Json数组

    [ { "type": "123", "value": 123 }, { "type": "234" ...

  4. 【Gson】互相转化

    Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. 对象转为字符串 Strin ...

  5. Android Gson解析

    目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),解析速度最快的是Gson,下载地址:https://co ...

  6. Android总结之json解析(FastJson Gson 对比)

    前言: 最近为了统一项目中使用的框架,发现项目中用到了两种json解析框架,他们就是当今非常主流的json解析框架:google的Gson 和阿里巴巴的FastJson,为了废除其中一个所以来个性能和 ...

  7. gson笔记 解析json数据

    gson中负责json数据解析的类是JsonReader. Json格式有两种结构,一种是对象(键值对的组合,无序),另外一种是数组(值的有序集合). 因此针对这两种格式,JsonReader提供了不 ...

  8. Android Gson的使用总结

    1.概念 Gson是谷歌发布的一个json解析框架 2.如何获取 github:https://github.com/google/gson android studio使用 compile 'com ...

  9. Android JSON、GSON、FastJson的封装与解析

    声明: 1.本帖只提供代码,不深入讲解原理.如果读者想要深入了解,那就不要在这个帖子上浪费时间了 2.客户端用的是Google官方的Volley访问服务器,具体了解Volley请戳 这里 3.本帖三种 ...

  10. Java Gson 简要笔记

    Gson是Google开发的Java比较好用的 Json工具. 使用挺简单,假设有个类: class Runner { int attr; String name; public Runner(int ...

随机推荐

  1. hdoj 3065 病毒侵袭持续中(AC自动机)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 思路分析:问题需要模式匹配多个模式串,需要注意的是模式串会包含和重叠,需要对AC自动机的匹配过 ...

  2. Swift与Objective-C API的交互

    互用性是让 Swift 和 Objective-C 相接合的一种特性,使你能够在一种语言编写的文件中使用另一种语言.当你准备开始把 Swift 融入到你的开发流程中时,你应该懂得如何利用互用性来重新定 ...

  3. 一些常用的Intent及intent-filter的信息

    Uri Action 功能 备注 geo:latitude,longitude Intent.ACTION_VIEW 打开地图应用程序并显示指定的经纬度   geo:0,0?q=street+addr ...

  4. 使用LINQ的几个小技巧

    这里总结了这些技巧.介绍如何使用LINQ来: 初始化数组 在一个循环中遍历多个数组 生成随机序列 生成字符串 转换序列或集合 把值转换为长度为1的序列 遍历序列的所有子集 如果你在LINQ方面有心得也 ...

  5. Decorator Pattern(装饰模式)

    装饰模式:动态的给一个对象添加一些额外的职责.当然我们也可以通过继承来实现类似的功能,但是随着子类的增多,各种子类的组合会造成子类的急剧膨胀. Requirement: 假设客户有一个要求,需要打一个 ...

  6. 使用autoCompleteTextView以及MultiAutoCompleteTextView实现自动匹配输入内容

    一:autoCompleteTextView 1:控件属性设置: 注意添加:android:completionThreshold="1"来设置输入几个字符的时候开始显示匹配的内容 ...

  7. Quiz 6b Question 7————An Introduction to Interactive Programming in Python

     Question 7 Convert the following English description into code. Initialize n to be 1000. Initiali ...

  8. 添加站点图标: 为SAE上的WordPress站点添加自己的Favicon

    由于插件 Jetpack的"添加站点图标"功能有问题, 无法从本地上传ico文件到SAE的Storage. 因此需要手动添加站点图标. Step 1: 制作或下载自己的ico文件, ...

  9. Android 开发技巧

    1, ActionBar.setTitle() 的参数是 CharSequence,可以用SpannableString的,颜色字形随你

  10. hibernate的配置 1

    hibernate 是一种ORM框架,是ORM框架中一个典范 ORM叫做对象关系映射 是面向对象语言和关系型数据库之间的映射关系 所以只有在面向对象语言或者关系型数据库没用的时候ORM才会消失 ORM ...