android中Gson解析

最近在做android项目,用到了Gson解析,总结一下放在这里,方便以后使用


服务器端代码:

package com.sdufe.domain;

/**
 * @author lili.guo
 * 
 *         2014-6-10下午2:20:31
 */
public class People {

	private int id;
	private String username;
	private String address;

	/**
	 * 无参构造函数
	 */
	public People() {
		// TODO Auto-generated constructor stub
	}

	public People(int id, String username, String address) {
		this.id = id;
		this.username = username;
		this.address = address;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}

package com.sdufe.tools;

import com.google.gson.Gson;

/**
 * @author lili.guo
 *
 * 2014-6-10下午2:39:47
 */
public class JsonTools {
	
	/**
	 * 将普通的字符串转化为json
	 * @param value
	 * @return
	 */
	public static String CreateJsonStringbyGson(String value){
		Gson gson=new Gson();
		String json=gson.toJson(value);
		return json;
	}

}

package com.sdufe.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sdufe.domain.People;

/**
 * @author lili.guo
 *
 * 2014-6-10下午2:27:36
 */
public class JsonService {
	
	public People getPeople(){
		People people=new People(1001, "Thea", "beijing");
		return people;
	}
	
	public List<People> getList(){
		List<People> list=new ArrayList<People>();
		list.add(new People(1001, "Thea", "beijing"));
		list.add(new People(1001, "Thea", "beijing"));
		list.add(new People(1001, "Thea", "beijing"));
		return list;
	}
	
	public List<String> getString(){
		List<String> list=new ArrayList<String>();
		list.add("beijing");
		list.add("shanghai");
		return list;
	}
	
	public List<Map<String, Object>> getListMap(){
		List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
		Map<String, Object> map1=new HashMap<String, Object>();
		map1.put("id", 1001);
		map1.put("username", "Thea");
		map1.put("address", "beijing");
		list.add(map1);
		Map<String, Object> map2=new HashMap<String, Object>();
		map2.put("id", 1001);
		map2.put("username", "Thea");
		map2.put("address", "beijing");
		list.add(map2);
		return list;
	}

}

其中jsonService中转化的数据类型比较多,差不多经常用到的几种都放在这里了


客户端代码:

package com.sdufe.tools;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdufe.domain.People;

/**
 * @author lili.guo
 * 
 *         2014-6-10下午2:53:16
 */
public class GsonTool {

	public static People getPeople(String jsonString) {

		Gson gson = new Gson();
		People people = gson.fromJson(jsonString, People.class);
		return people;
	}

	public static List<People> getList(String jsonString) {

		Gson gson = new Gson();
		List<People> list=new ArrayList<People>();
		list=gson.fromJson(jsonString, new TypeToken<List<People>>() {
		}.getType());
		return list;
	}
	
	public static List<String> getString(String json){
		
		Gson gson=new Gson();
		List<String> list=new ArrayList<String>();
		list=gson.fromJson(json, new TypeToken<List<String>>() {
		}.getType());
		return list;
	}
	
	public List<Map<String, Object>> getListMap(String json){
		List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
		Gson gson=new Gson();
		list=gson.fromJson(json, new TypeToken<List<Map<String,Object>>>() {
		}.getType());
		return list;
	}

}

稍微解释一下

服务器端负责把String类型的字符串转化成json类型,而客户端则负责把json类型解析成普通的String类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值