Gson进行json字符串和对象之间的转化
Gson可以实现对象与json字符串之间的转化,以下是在Android中的示例代码。
Gson主页:https://code.google.com/p/google-gson/
public class GsonActivity extends Activity {
Button saveButton;
Button loadButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gsonlayout);
initControls();
}
protected void initControls(){
saveButton = (Button) findViewById(R.id.btSave);
loadButton = (Button) findViewById(R.id.btGsonLoad);
saveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
List<StudentInfo> studentInfos = new LinkedList<StudentInfo>() ;
StudentInfo s1 = new StudentInfo();
s1.setId(1);
s1.setName("张三");
s1.setAddress("武汉市");
s1.setPhone("12345671");
studentInfos.add(s1);
StudentInfo s2 = new StudentInfo();
s2.setId(2);
s2.setName("李四");
s2.setAddress("华工");
s2.setPhone("12345672");
studentInfos.add(s2);
Gson gson = new Gson();
String json = gson.toJson(studentInfos);
try {
FileOutputStream fs = openFileOutput("gsonconfig.xml", MODE_PRIVATE);
fs.write(json.getBytes());
fs.close();
Toast.makeText(GsonActivity.this, json, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(GsonActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
loadButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
List<StudentInfo> studentInfos = new LinkedList<StudentInfo>();
String json = "";
try {
FileInputStream fileInputStream = openFileInput("gsonconfig.xml");
InputStreamReader inputStreamReader =new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
json = bufferedReader.readLine();
bufferedReader.close();
Gson gson = new Gson();
studentInfos = gson.fromJson(json, new TypeToken<List<StudentInfo>>() {
}.getType());
for (StudentInfo studentInfo : studentInfos) {
Toast.makeText(GsonActivity.this, studentInfo.toString(), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
更多例子可以参见http://blog.csdn.net/lk_blog/article/details/7685169
Gson进行json字符串和对象之间的转化的更多相关文章
- Google Gson实现JSON字符串和对象之间相互转换
User实体类 package com.test.json; /** * User 实体类 */ public class User { private String name; private St ...
- C# .net中json字符串和对象之间的转化方法
http://blog.csdn.net/xuexiaodong009/article/details/46998069 json作为作为一种最常用的数据,应用很广泛,在.net中如何把一个对象转化为 ...
- 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换
[源码下载] 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换 作者:webabcd 介绍速战速决 之 PHP ...
- JSON字符串和对象之间的转换
JSON(JavaScript Object Notation) 是JavaScript编程语言的一个子集.正因JSON是JavaScript的一个子集,所以它可清晰的运用于此语言中. eval函数 ...
- json字符串和对象的相互转化
json在代码中是经常用到的,在此总结一下json字符串和对象及数组之间的相互转化: 1.javascript函数方式: <1> JSON.stringify :把一个对象转换成json字 ...
- json、xml和java对象之间的转化
其实从面相对象的角度来理解这个问题,就会很清晰.java中的一切皆对象即把世间万物(Everything in the world)看做java对象,任何处理不了的问题都可以先转化成java对象在做处 ...
- Gson转换json数据为对象
可以通过Gson使用两种方法,将json字符串转换为对象,以下面该段报文做测试 { "id": 84041462, "lastName": "小华&q ...
- JSON 与JAVA对象之间的转换(转)
JSON与XML两者目前在数据交互方面都有很高的使用率,随着现在SOA的兴起,异构系统的整合地位相应提高,本篇文章主要介绍JSON与JAVA对象之间的相互转换. 一.对普通类型的JSON模式的转换 一 ...
- js在前端json字符串和对象互相转化
js在前端json字符串和对象互相转化 //对象转json串 注意:参数必须是对象,数组不起作用,对象格式{'0'=>'a'} JSON.stringify( arr ); //json字符串转 ...
随机推荐
- hdu 5183 hash表
BC # 32 1002 题意:给出一个数组 a 和一个数 K ,问是否存在数对( i , j ),使 a i - a i + 1 +……+ (-1)j - i a j : 对于这道题,一开始就 ...
- test20181015 B君的第二题
题意 分析 考场85分 用multiset暴力,由于教练的机子飞快,有写priority_queue水过了的人. #include<cstdlib> #include<cstdio& ...
- (考研)散列表和hashcode和hashmap
package tt; import java.util.HashMap; import java.util.Map; public class a0 { public static void mai ...
- (精)字符串,map -> json对象->map(初学者必读)
import java.util.LinkedList; import java.util.*; import java.util.ListIterator; import net.sf.json.J ...
- 关于jdbc的面试题
什么是JDBC,在什么时候会用到它? JDBC的全称是Java DataBase Connection,也就是Java数据库连接,我们可以用它来操作关系型数据库.JDBC接口及相关类在java.sql ...
- oracle nodejs 访问
1. 下载node-oracle网址如下: https://github.com/joeferner/node-oracle 2. 测试代码: var oracle = require('oracle ...
- hibernate 1-1(具体解释)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/27870343 域模型 关系数据模型: 依 ...
- SharedImageCollection
https://documentation.devexpress.com/#WindowsForms/clsDevExpressUtilsSharedImageCollectiontopic The ...
- Xamarin版的C# SVG路径解析器
原文:Xamarin版的C# SVG路径解析器 Xamarin版的C# SVG路径解析器,对SVG的Path路径进行解析,其中包括: 主程序SvgPathParser.cs, 相关接口定义:ISour ...
- centos下yum安装pip失败
[root@wfm ~]# yum -y install pip Loaded plugins: fastestmirror, refresh-packagekit, securityLoading ...