Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[疑惑]:为啥fastjson解析的是正常的,本框架解析的不一样 #40

Closed
JasonYinH opened this issue Mar 11, 2024 · 12 comments
Closed
Labels
question Further information is requested

Comments

@JasonYinH
Copy link

问题描述【必填】

比如字段 attitude、collectTime

image
image
image
image
image

框架文档是否提及了该问题【必答】

是否已经查阅框架文档但还未能解决的【必答】

issue 列表中是否有人曾提过类似的问题【必答】

是否已经搜索过了 issue 列表但还未能解决的【必答】

@JasonYinH JasonYinH added the question Further information is requested label Mar 11, 2024
@JasonYinH
Copy link
Author

今天想着替换fastJson的,框架版本都是最新的
image

@getActivity
Copy link
Owner

小伙子,我无法复制你图片上面的代码,另外你图片上面的代码不完整,请提供完整且可复现的代码到这里来。

@JasonYinH
Copy link
Author

JasonYinH commented Mar 11, 2024

  • assets 文件里的资源
{
  "attitude": 2,
  "collectTime": 1710150586000
}
  • activity 代码
  val json: String = getAssetsString(this, "AbnormalJson.json")
        val mGson = GsonFactory.getSingletonGson()
        val jsonBean = mGson.fromJson(json, aaaa::class.java)
        Log.e("xxx", "" + mGson.toJson(jsonBean))

        val beanToMap = mGson.fromJson(mGson.toJson(jsonBean), MutableMap::class.java)
        Log.e("xxxbb", "" + beanToMap)
  • aaaa 类
class aaaa {
    private var attitude: Int = 0
    private var collectTime: Long = 0L
    override fun toString(): String {
        return "aaaa(attitude=$attitude, collectTime=$collectTime)"
    }
}

@JasonYinH
Copy link
Author

image

@JasonYinH
Copy link
Author

https://mikechen.cc/28914.html 用的第一种

@JasonYinH
Copy link
Author

image
要是用这种反射的方法,将对象转为map就没事了

@getActivity
Copy link
Owner

image image

@getActivity
Copy link
Owner

image

@getActivity
Copy link
Owner

getActivity commented Mar 11, 2024

小伙子,经过核实和验证,确实有这个问题,这是由于 Gson 内部将不指定类型的数值统一解析成 double 类型导致的,1710126441000 这个数值会被科学计数法转换成 1.71E+12,但是不代表这样的数值有问题,这两种计数严格意义上讲都没有问题,都是相等的,只不过你更想要显示全数值的那种,而非科学计数值,解决这个问题也很简单,我在框架内部自定义了 ToNumberStrategy 接口策略,对后台返回的数值接收类型进行了优化,如果是浮点数,则用 double 类型装载,如果是整数,则用 int 类型或者 long 类型装载。

image

@getActivity
Copy link
Owner

getActivity commented Mar 11, 2024

GsonFactory-9.6.aar.zip

@getActivity
Copy link
Owner

小伙子,你可以用上面的 aar 包试一下看问题是否被解决?

@JasonYinH
Copy link
Author

佬,用9.6的包解决了,感谢佬的解决

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants