前提是需要加Jar包:
gson-2.2.2.jar
package utils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.dll.WebServiceParams;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
public class GSonUtils {
/**
* 将List<Map<String,Object>>转换成JSON字符串
*
* @param o
* @return
*/
public static String parseListMap2JSon(Object o) {
Gson gson = new GsonBuilder().create();
return gson.toJson(o, new TypeToken<List<Map<String, Object>>>() {
}.getType());
}
/**
* 将JSON格式转换成Map
*
* @param json
* @return
*/
public static Map<String, Object> parseJSON2Map(String json) {
Map<String,Object> map = new HashMap<String, Object>();
Gson gson = new GsonBuilder().create();
JsonReader reader = new JsonReader(new StringReader(json));
map = gson.fromJson(reader, new TypeToken<Map<String, Object>>() {
}.getType());
return map;
}
/**
* 将JSON转成List<Map>
*
* @param json
* @return
*/
public static List<Map<String, Object>> parseJSON2ListMap(String json) {
List<Map<String,Object>> rst = new ArrayList<Map<String,Object>>();
Gson gson = new Gson();
JsonReader reader = new JsonReader(new StringReader(json));
rst = gson.fromJson(reader, new TypeToken<List<Map<Object,String>>>() {
}.getType());
return rst;
}
/**
* 将对象转换成JSON格式
* @param o
* @return
*/
public static String parse2JSON(Object o) {
if(o == null){
return "";
}
Gson g = new GsonBuilder().create();
String json = g.toJson(o,o.getClass());
return json;
}
/**
* 将json字符串转换成指定类型的对象
* @param json
* @param typeToken
* @return
*/
public static <T> T parseJson2(String json, TypeToken<T> typeToken){
Gson gson = new Gson();
JsonReader reader = new JsonReader(new StringReader(json));
return gson.fromJson(reader, typeToken.getType());
}
public static WebServiceParams parseJson2WebServiceParams(String json){
return GSonUtils.parseJson2(json, new TypeToken<WebServiceParams>(){});
}
}
================demo案例json互转类型===============================
String infos = test();//json map类型格式:{"pubuser":"{\"code\":\"test\",\"dateRegister\":\"2013-09-24 14:11:46.0\",\"password\":\"c4ca4238a0b923820dcc509a6f75849b\",\"id\":\"e6ee44f3-98ab-4446-bd9b-db2e525d3b24\",\"serialNum\":148,\"status\":0}","info":"{\"dbVersion\":22}"}
//解析json map格式
Map<String, String> map = parseJson2(infos,new TypeToken<Map<String, String>>() {});// json==》map
String pubuser = map.get("pubuser");
Pubuser pubs = parseJson2(pubuser, new TypeToken<Pubuser>() {
});// json==》Pubuser(实体对象)
//实体对象转换成json map格式
public String test(){
Map<String,String> map = new HashMap<String,String>();
Tradings data = new Tradings();//实体对象
data.setId(UUID.randomUUID().toString());
data.setCost(19.2);
map.put("trading", parse2JSON(data));
return parse2JSON(map);
}
/**
* 将对象转换成JSON格式
*
* @param o
* @return
*/
public static String parse2JSON(Object o) {
if (o == null) {
return "";
}
Gson g = new GsonBuilder().create();
String json = g.toJson(o, o.getClass());
return json;
}
/**
* 将json字符串转换成指定类型的对象
*
* @param json
* @param typeToken
* @return
*/
public static <T> T parseJson2(String json, TypeToken<T> typeToken) {
Gson gson = new Gson();
JsonReader reader = new JsonReader(new StringReader(json));
return gson.fromJson(reader, typeToken.getType());
}
/**
* 将JSON转成List<Map>
*
* @param json
* @return
*/
public static List<Map<String, Object>> parseJSON2ListMap(String json) {
List<Map<String, Object>> rst = new ArrayList<Map<String, Object>>();
Gson gson = new Gson();
JsonReader reader = new JsonReader(new StringReader(json));
rst = gson.fromJson(reader, new TypeToken<List<Map<Object, String>>>() {
}.getType());
return rst;
}