前置添加pom
com.google.code.gson
gson
2.2.4
1、json转换成String
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
class JsonUtil{
public static StringobjectToJsonString(Object obj) {
return createGsonSetDateFormat("yyyy-MM-dd HH:mm:ss").toJson(obj);
}
private static GsoncreateGsonSetDateFormat(String dateFormat) {
return (new GsonBuilder()).setDateFormat(dateFormat).create();
}
}
2、String(json)转换成json
Gson gson = new Gson();
Map map = new HashMap();
map = gson.fromJson(value, map.getClass());
System.out.println(map.toString());
String goodsid=(String) map.get("event");
System.out.println("map的值为:"+goodsid);