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

JSONObject.getObject(String, Class)方法不能自动适配数据类型 #3274

Closed
Dollyn opened this issue Jun 16, 2020 · 1 comment
Closed
Milestone

Comments

@Dollyn
Copy link

Dollyn commented Jun 16, 2020

如下所示,如果直接 JSON.parseObject()是可以自动把"1"转成int的,

但如果不定义外层的类,先解析为通用的JSONObject,再用getObject()进行局部的转换的时候,就不会做自动的转换了,同样的,@JSONField(format="")也不再起作用

class TestFJ {

    @Test
    fun test() {
        val str = """
            {"data": {"id": "1", "name":"n1"}}
        """.trimIndent()


        val d1 = JSON.parseObject(str, Data2::class.java)

        val data = JSON.parseObject(str)
        val d2 = data.getObject("data", Data::class.java)

        Assert.assertEquals(1, d1.data.id)
        Assert.assertEquals(1, d2.id)
    }
}

data class Data(
        val id: Int = 0,
        val name: String = "",
        val date: Date? = null
)
data class Data2(
        val data: Data
)
@wenshao wenshao added this to the 1.2.72 milestone Jun 25, 2020
@wenshao
Copy link
Member

wenshao commented Jun 27, 2020

https://github.com/alibaba/fastjson/releases/tag/1.2.72
问题已修复,请用新版本

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

No branches or pull requests

2 participants