【问题标题】:FlexJSON - JSONDeserializer String Cannot be caste as BooleanFlexJSON - JSONDeserializer 字符串不能作为布尔值
【发布时间】:2012-02-05 14:38:52
【问题描述】:

我在我的游戏框架应用程序中使用 FlexJson,但此时我试图反序列化 json 字符串,它会抛出 java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean

    User user = new JSONDeserializer<User>()
        .use(null, User.class).deserialize(body);

Body 是使用标准 jquery/ajax 传入控制器的 json 字符串, 其中 User 声明了以下布尔值:

    public Boolean isCurrentUser;

关于我做错了什么有什么想法吗?

谢谢

【问题讨论】:

  • 你能附上你试图反序列化的 Json 字符串吗?
  • 嗨,json字符串是:{"user_id":"18","isCurrentUser":"true","title":"mr","description":"description"}跨度>

标签: json playframework flexjson


【解决方案1】:

在 Json 中,布尔值是一种类型。你的 JSON 是:

{"user_id":"18","isCurrentUser":"true","title":"mr","description":"description"} 

什么时候应该:

{"user_id":"18","isCurrentUser":true,"title":"mr","description":"description"} 

请注意,true 不是字符串,而是布尔值。解析器失败,因为它找到了一个字符串而不是预期的布尔类型。修复 JSON 生成以添加布尔值,而不是字符串。

【讨论】:

  • 啊哈,这是有道理的。我会试一试。谢谢
猜你喜欢
  • 2021-05-07
  • 1970-01-01
  • 2011-10-29
  • 2016-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
相关资源
最近更新 更多