美文网首页Kotlinkotlin程序员
68. (android开发) Gson的坑之关于CreatTi

68. (android开发) Gson的坑之关于CreatTi

作者: 厚土火焱 | 来源:发表于2018-05-01 21:24 被阅读35次

每一个使用 Kotlin 的在进行服务端访问的时候,json 解析免不了要使用
Gson,今天偶尔发现了一个奇怪的 CreatTime 坑。在坑里待了 3 天,终于出坑了。
首先比较一下下面两段代码的差别
代码一:

for (i in 0..jsonObject.length() - 1){
                    val managerInfo = jsonObject.getJSONObject(i).toString()
                    val mmtemp = Gson().fromJson(managerInfo, ManagerPower::class.java)
                    try {
                        dataList.add(ManagerPower(mmtemp.Id, mmtemp.PowerCode, mmtemp.PowerName, mmtemp.PowerInfo, mmtemp.OrderNum, mmtemp.PowerGroupCode, mmtemp.CreateTime, mmtemp.Creator, mmtemp.UpdateTime, mmtemp.Updator))
                        Log.d("joel test --> ", mmtemp.CreateTime)
                    }catch (e : Exception){
                        Log.d("joel error --> ", "ManagerPowerEditActivity.kt line 98 \r\n"+e.toString() + "\r\n mmtemp.CreatTime is " + mmtemp +"\r\n" + managerInfo.toString())
                    }
                }

代码二:

for (i in 0..jsonObject.length() - 1){
                    val managerInfo = jsonObject.getJSONObject(i).toString()
                    val mmtemp = Gson().fromJson(managerInfo, ManagerPower::class.java)
                    try {
                        dataList.add(ManagerPower(mmtemp.Id, mmtemp.PowerCode, mmtemp.PowerName, mmtemp.PowerInfo, mmtemp.OrderNum, mmtemp.PowerGroupCode, mmtemp.CreateTime, mmtemp.Creator, mmtemp.UpdateTime, mmtemp.Updator))
                        Log.d("joel test --> ", mmtemp.CreateTime)
                    }catch (e : Exception){
                        Log.d("joel error --> ", "ManagerPowerEditActivity.kt line 98 \r\n"+e.toString() + "\r\n mmtemp.CreateTime is " + mmtemp +"\r\n" + managerInfo.toString())
                    }
                }

这两段代码的差别,只在 mmtemp.CreateTime 和 mmtemp.CreatTime 上,其他的一模一样。一个可以正常运行解析 json ,一个不可以运行。
mmtemp.CreatTime 字段的值在 fromJson 时会给出 null 值。

相关文章

  • 68. (android开发) Gson的坑之关于CreatTi

    每一个使用 Kotlin 的在进行服务端访问的时候,json 解析免不了要使用Gson,今天偶尔发现了一个奇怪的 ...

  • Android Gson使用详解

    Android Gson使用详解(出处: 安卓巴士 - 安卓开发 - Android开发 - 安卓 - 移动互联网门户)

  • Gson序列化那些事

    Android开发中会经常解析json,使用retrofit框架常用gson解析json。Gson有一定的容错机制...

  • 谈谈关于Android视频编码的那些坑

    本文讲的是谈论关于Android视频编码的那些坑,Android的视频相关的开发,大概一直是整个Android生态...

  • android开发学习之Gson解析

    最近在学习一些新的知识,试着用MVP模式写项目。以前在项目中使用的都是JsonObject和JsonArray来解...

  • Android 库 Gson

    【Android 库 Gson】 引用: ★Gson 解析教程★★★ Gson的入门使用Gson全解析(上)-Gs...

  • ProGuard混淆Exception

    转:Android-Exception整理之com.google.gson.internal.xxx cannot...

  • LibManager文档

    LibManager 项目介绍 Android IOT开发框架Http网络请求、Gson、蓝牙连接、适配器、NFC...

  • 关于Gson的几个坑

    为何有些序列化的结果出乎你想象? 废话不多说,直接上正文 坑1:我们初始化Map之类的集合的时候会用如下优雅的方式...

  • android项目框架大集合

    自己整理的代码快速开发框架代码见github FrameDemo android项目框架 1.实体生成 Gson、...

网友评论

  • 小武说:土哥,这句`val mmtemp = Gson().fromJson(managerInfo, ManagerPower::class.java)`是否需要json和类定义字段匹配才能解析?
    厚土火焱:@小武说 managerInfo, ManagerPower::class.java 这两个要结构相同才行

本文标题:68. (android开发) Gson的坑之关于CreatTi

本文链接:https://www.haomeiwen.com/subject/aqhfrftx.html