虽然android自带了json处理,但是没有封装,总觉得不方便,网上找了gson的处理,觉得还行。
//转换器
GsonBuilder builder = new GsonBuilder();
// 不转换没有 @Expose 注解的字段
builder.excludeFieldsWithoutExposeAnnotation();
Gson gson = builder.create();
//1、对象转string
Student stu = new Student();
stu.setStudentId(333);
stu.setStudentName("qqq");
String stuStr = gson.toJson(stu);
System.out.println(stuStr); //{"studentName":"qqq","studentId":333}
//2、string转对象
Student user2 = gson.fromJson(stuStr, Student.class);
System.out.println(user2);
String stuTemp = "{\"studentName\":\"qqq2\",\"studentId\":3335}";
Student user4 = gson.fromJson(stuTemp, Student.class);
System.out.println(user4);
//3、对象List转string
List<Student> testBeanList = new ArrayList<Student>();
Student testBean = new Student();
testBean.setStudentId(555);
testBean.setStudentName("552");
testBeanList.add(testBean);
//Gson gsonList = new Gson();
Type type = new TypeToken<List<Student>>(){}.getType(); //指定集合对象属性
String beanListToJson = gson.toJson(testBeanList, type);
System.out.println(beanListToJson); //[{"studentName":"552","studentId":555}]
//集合string转对象list
List<Student> testBeanListFromJson = gson.fromJson(beanListToJson, type);
System.out.println(testBeanListFromJson); //[555:552]
//4、集合如果不指定类型 默认为String
List<String> testList = new ArrayList<String>();
testList.add("first");
testList.add("second");
String listToJson = gson.toJson(testList);
System.out.println(listToJson); //["first","second"]
//5、集合字符串转回来需要指定类型
List<String> testList2 = (List<String>) gson.fromJson(listToJson,
new TypeToken<List<String>>() {
}.getType());
System.out.println(testList2);
//6、 将HashMap字符串转换为 JSON
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("id", "id.first");
testMap.put("name", "name.second");
String mapToJson = gson.toJson(testMap);
System.out.println(mapToJson); //{"id":"id.first","name":"name.second"}
//7、stringMap转对象
Map<String, String> userMap2 = (Map<String, String>) gson.fromJson(mapToJson,
new TypeToken<Map<String, String>>() {
}.getType());
System.out.println(userMap2); //{id=id.first, name=name.second}
//8、对象含有普通对象、集合、map情况
Student user1 = new Student();
user1.setStudentId(1001);
user1.setStudentName("张三");
Student user3 = new Student();
user3.setStudentId(1002);
user3.setStudentName("李四");
Map<String, Student> userMap = new HashMap<String, Student>();
userMap.put("user1", user1);
userMap.put("user3", user3);
List<Student> userList = new ArrayList<Student>();
userList.add(user1);
userList.add(user3);
Teacher groupBean = new Teacher();
groupBean.setStudent(user1);
groupBean.setStus(userList);
groupBean.setMap((HashMap)userMap);
//groupBean.setUserList(userList);
Gson gsonGroup = new Gson();
String sGroupBean = gsonGroup.toJson(groupBean, new TypeToken<Teacher>() {
}.getType());
System.out.println(sGroupBean);
/*{"stus":[{"studentName":"张三","studentId":1001},{"studentName":"李四","studentId":1002}],"student":{"studentName":"张三","studentId":1001},"map":{"user3":{"studentName":"李四","studentId":1002},"user1":{"studentName":"张三","studentId":1001}},"id":0,"age":0}*/
//9、复杂对象string转对象
Teacher groupBean2 = (Teacher) gson.fromJson(sGroupBean,
new TypeToken<Teacher>() {
}.getType());
System.out.println(groupBean2);
package com.andtools;
import com.google.gson.annotations.Expose;
public class Student {
@Expose
private String studentName;
@Expose
private int studentId;
public Student(){}
public Student(int studentId,String studentName){
this.setStudentId(studentId);
this.setStudentName(studentName);
}
public String toString(){
return this.getStudentId() + ":" + this.getStudentName();
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
}
package com.andtools;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.annotations.Expose;
public class Teacher {
@Expose
private int id;
@Expose
private String name;
@Expose
private int age;
@Expose
private Student student;
@Expose
private List stus;
@Expose
private HashMap map;
public String toString(){
return this.getId()+":"+this.getName()+":"+this.getAge() +":"+ this.getStudent().toString() + ":" + this.getStus() + ":" + this.getMap();
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List getStus() {
return stus;
}
public void setStus(List stus) {
this.stus = stus;
}
public HashMap getMap() {
return map;
}
public void setMap(HashMap map) {
this.map = map;
}
}
附件为网上找的文档。
分享到:
相关推荐
Android Studio下使用Google Gson解析Json数据示例
Android基于Gson序列化和反序列化,用于存储对象到xml和从xml中解析对象
android google gson json解析
Android Gson解析案例,可变长数据解析
Eclipse下使用Google Gson解析Json数据示例,挺好用的,亲测
原生态打造android中获取json数据和解析json数据的例子。使用的android中自带的gson,不过可能会不稳定有时候
android之json和gson数据解析最完整的代码例子(包括各种样式的json数据) 本人在2.3.3模拟器上已亲测可以运行。最适合学习json 或gson数据解析的初学者。
Eclipse下使用Google Gson解析Json数据示例
Volley网络框架,有jar及使用例子,可使用Gson
解放你的双手,不用再去一个一个JSONObject去处理解析问题,两行代码搞定一切
一个Android示例代码,使用Android的JSONObject和Google的gson在json和java对象之间转换数据。 此示例代码基于并经过修改以测试json和带有嵌套对象的java对象之间的转换。 创建并使用了三个自定义类: 相册(包含...
java Gson 与map互相转换的小demo示例
在Android和GSON上使用小示例。 您还可以按照《 的 使用以下User模型: @Gson.TypeAdapters @Value.Immutable public abstract class User { public abstract int id(); public abstract String username(); } ...
Android 使用Gson 跨平台传输对象
对Gson数据和服务器交互视使用示例,在移动开发中,Json数据格式一直是程序员们经常用到的,特别是在服务器和客户端交互时,Gson数据的使用大大降低了硬件资源消耗。
android-目录列表-java-realm-gson-retrofit 由迈克尔·斯蒂尔(Michael Steele)在17年3月13日创建。 版权所有:copyright:2017 Michael Steele。 版权所有。 进行中的开发-使用Java,Realm,Retrofit2,Glide,SSL...
Gson 解析器示例 如何使用 Gson 解析 Json 文档 1.- 在 Android Studio 中下载 Gson 库。 在 app 文件夹中,我们打开 build.gradle 文件并在“dependencies”部分添加以下行: compile '...
android-restlet 一个非常简单的演示/教程,展示了如何在Android下使用Restlet和Gson库这是什么? 这是一个项目教程,可帮助您了解如何使用Restlet框架( )以及如何通过使用Gson Google框架( )。 您可以将该项目...
MovieFinder使用MVVM-Android::fire:MVVM +简洁架构+最佳做法| vieMovie Finder是一个示例Android应用程序:mobile_phone:使用OMDb API搜索电影,该API旨在演示现代Android开发工具的使用-(Kotlin,协程,科丁,...
有关如何使用翻新的示例方法 依存关系 appcompat-v7:21.0.3 改造:1.9.0 okhttp-urlconnection:2.0.0 okhttp:2.0.0 将此项目导入Intellij Idea 然后在RestClient.java文件中进行更改 private static String ...