轉換我創建了以下節儉對象:節儉 - 從簡單的JSON
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。我有非常簡單的節儉結構,不需要地圖和列表。 –
問題是你使用的工具比你需要的更復雜(並且理解)。開始簡單。 –