首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >节约不能从json反序列化到java对象。

节约不能从json反序列化到java对象。
EN

Stack Overflow用户
提问于 2014-06-20 03:05:37
回答 1查看 5.4K关注 0票数 1

我从以下节俭对象生成了一个java对象:

代码语言:javascript
复制
struct Account {
    1: required string accountType,
    2: bool accountActive,
}

我编写了一个java代码,试图将java对象序列化为json字符串,然后反序列化json字符串到java对象。我可以成功地序列化,但是反序列化失败。

代码语言:javascript
复制
    TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory());
    TDeserializer deserializer = new TDeserializer(new TSimpleJSONProtocol.Factory());

    Account a1 = new Account();
    a1.setAccountType("P");
    a1.setAccountActive(true);

    String json = serializer.toString(a1);
    System.out.println(json);

    Account a2 = new Account();
    deserializer.deserialize(a2, json, "UTF-8");
    System.out.println(a2);
    System.out.println(a2.getAccountType());

它一直抛出以下异常:

代码语言:javascript
复制
Exception in thread "main" org.apache.thrift.protocol.TProtocolException: Required field 'accountType' was not present! Struct: Account(accountType:null, accountActive:false)

有人能帮我找出什么问题吗?提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2014-06-20 10:11:29

SimpleJSONProtocol从来没有打算被反序列化。使用TJSONProtocol代替。

来自http://wiki.apache.org/thrift/ThriftUsageJava

序列化为“简单”JSON TSerializer序列化程序=新TSerializer(新TSimpleJSONProtocol.Factory());字符串json = serializer.toString(work); 这个“简单”的JSON协议产生了适合于AJAX或脚本语言的输出。它不保存Thrift的字段标记,并且不能被读回。

(强调地雷)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24319325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档