前置添加pom
<!-- 配置gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
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<String, Object> map = new HashMap<String, Object>();
map = gson.fromJson(value, map.getClass());
System.out.println(map.toString());
String goodsid=(String) map.get("event");
System.out.println("map的值为:"+goodsid);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)