object JsonUtils {
private val TAG = JsonUtils::class.java.name
private val GSON_NO_NULLS = createGson(false) // 序列化,字段内容为空时,是否还需要这个Key
private fun createGson(serializeNulls: Boolean = true): Gson {
val builder = GsonBuilder()
builder.registerTypeAdapter(Date::class.java, DateFormatter())
builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
//Using this naming policy with Gson will ensure that the field name is unchanged.
//the default type(so,it can omit)
builder.setFieldNamingPolicy(FieldNamingPolicy.IDENTITY)
if (serializeNulls) {
builder.serializeNulls()
}
return builder.create()
}
fun getGson(): Gson {
return createGson(true)
}
@JvmOverloads
fun toJson(obj: Any, includeNulls: Boolean = true): String {
return if (includeNulls) getGson().toJson(obj) else GSON_NO_NULLS.toJson(obj)
}
fun <V> fromJson(json: String, type: Class<V>): V {
return getGson().fromJson(json, type)
}
fun <V> fromJson(json: String, type: Type): V {
return getGson().fromJson(json, type)
}
fun <V> fromJson(jsonElement: JsonElement, type: Type): V {
return getGson().fromJson(jsonElement, type)
}
fun <V> fromJson(jsonElement: JsonElement, type: Class<V>): V {
return getGson().fromJson(jsonElement, type)
}
fun <V> fromJson(params: Map<String, Any>, type: Class<V>): V {
return getGson().fromJson(getGson().toJson(params), type)
}
fun <V> fromJson(params: Map<String, Any>, type: Type): V? {
return getGson().fromJson<V>(getGson().toJson(params), type)
}
fun <V> fromJson(reader: Reader, type: Class<V>): V {
return getGson().fromJson(reader, type)
}
fun <V> fromJson(reader: Reader, type: Type): V {
return getGson().fromJson(reader, type)
}
fun <V> fromJson(reader: JsonReader, type: Class<V>): V {
return getGson().fromJson(reader, type)
}
fun <V> fromJson(reader: JsonReader, type: Type): V {
return getGson().fromJson(reader, type)
}
fun optString(json: String?, key: String): String {
return JSONObject(json).optString(key)
}
fun optBoolean(json: String?, key: String): Boolean {
return JSONObject(json).optBoolean(key)
}
fun optInt(json: String?, key: String): Int {
return JSONObject(json).optInt(key)
}
fun jsonToMap(obj: JSONObject): Map<String, String> {
val map = HashMap<String, String>()
val iterator = obj.keys()
while (iterator.hasNext()) {
val key = iterator.next()
map[key] = obj.opt(key).toString()
}
return map
}
class DateFormatter @SuppressLint("SimpleDateFormat")
constructor() : JsonDeserializer<Date>, JsonSerializer<Date> {
private val formats = arrayOfNulls<DateFormat>(1)
init {
this.formats[0] = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val timeZone = TimeZone.getTimeZone("Zulu")
val var2 = this.formats
val var3 = var2.size
(0 until var3)
.map { var2[it] }
.forEach { it?.timeZone = timeZone }
}
@Throws(JsonParseException::class)
override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): Date? {
val value = json.asString
if (!TextUtils.isEmpty(value) && value.length != 1) {
val var5 = this.formats
val var6 = var5.size
var var7 = 0
while (var7 < var6) {
val format = var5[var7]
if (format != null) {
try {
synchronized(format) {
return format.parse(value)
}
} catch (var12: ParseException) {
android.util.Log.e(TAG, "日期转换错误, $value", var12)
++var7
}
}
}
return Date(0L)
} else {
return null
}
}
override fun serialize(date: Date, type: Type, context: JsonSerializationContext): JsonElement {
val primary = this.formats[0]
var formatted = ""
if (primary != null) {
synchronized(primary) {
formatted = primary.format(date)
}
}
return JsonPrimitive(formatted)
}
}
}
JsonUtils工具类 gson解析
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- json解析--Gson使用和json解析类详解 1.对于标准的json数据格式,可以使用Google 提供的用来...
- 参考资料:高教版《全国计算机等级考试二级教程——C语言程序设计》《21天学通C语言》 二维数组的定义和二维数组元素...