【发布时间】:2013-12-14 05:15:55
【问题描述】:
我创建了以下 Thrift 对象:
struct Student{
1: string id;
2: string firstName;
3: string lastName
}
现在我想从 JSON 中读取这个对象。根据这个post这是可能的
于是我写了如下代码:
String json = "{\"id\":\"aaa\",\"firstName\":\"Danny\",\"lastName\":\"Lesnik\"}";
StudentThriftObject s = new StudentThriftObject();
byte[] jsonAsByte = json.getBytes("UTF-8");
TMemoryBuffer memBuffer = new TMemoryBuffer(jsonAsByte.length);
memBuffer.write(jsonAsByte);
TProtocol proto = new TJSONProtocol(memBuffer);
s.read(proto);
我得到的是以下异常:
Exception in thread "main" org.apache.thrift.protocol.TProtocolException: Unexpected character:i
at org.apache.thrift.protocol.TJSONProtocol.readJSONSyntaxChar(TJSONProtocol.java:322)
at org.apache.thrift.protocol.TJSONProtocol.readJSONInteger(TJSONProtocol.java:698)
at org.apache.thrift.protocol.TJSONProtocol.readFieldBegin(TJSONProtocol.java:837)
at com.vanilla.thrift.example.entities.StudentThriftObject$StudentThriftObjectStandardScheme.read(StudentThriftObject.java:486)
at com.vanilla.thrift.example.entities.StudentThriftObject$StudentThriftObjectStandardScheme.read(StudentThriftObject.java:479)
at com.vanilla.thrift.example.entities.StudentThriftObject.read(StudentThriftObject.java:413)
at com.vanilla.thrift.controller.Main.main(Main.java:24)
我错过了什么吗?
【问题讨论】:
-
首先将您的 JSON 解析为地图和列表,然后转储并理解它。然后弄清楚如何提取你想要的数据。
-
我很抱歉,但我不明白你的意思。我应该解析什么json。我有非常简单的节俭结构,不需要地图和列表。
-
问题是您使用的工具比您需要(和理解)更复杂。从简单开始。
标签: java json thrift thrift-protocol