【发布时间】:2016-07-20 17:21:58
【问题描述】:
CreateOrderRequest req = new CreateOrderRequest();
req.pid=1111;
req.name="xxx";
TMemoryBuffer mem_buf = new TMemoryBuffer(512);
TJSONProtocol bin_proto = new TJSONProtocol(mem_buf,true);
try
{
req.write(bin_proto);
System.out.println("====begin serial json=================");
String jsonStr=mem_buf.toString("utf-8");
System.out.println("after jasonprocol="+jsonStr);
System.out.println("====begin deserial json=================");
CreateOrderRequest req2 = new CreateOrderRequest();
TMemoryBuffer mem_buf2 = new TMemoryBuffer(512);
byte[] data = jsonStr.getBytes();
mem_buf2.write(data);
TJSONProtocol bin_proto2 = new TJSONProtocol(mem_buf2,true);
req2.read(bin_proto2);
System.out.println("after de jasonprocol pid="+req.pid);
System.out.println("after de jasonprocol name="+req.name);
}
catch(Exception e)
{
System.out.println(e);
}
输出:
====begin serial json=================
after jasonprocol={"pid":{"i32":1111},"uid":{"i32":0},"corpId": {"i32":0},"sharePriceE6":{"i64":0},"shareCount":{"i32":0},"totalPriceE6":{"i64":0},"bankcardId":{"i32":0},"name":{"str":"xxx"},"isAnonymous":{"tf":0},"expectIncomeE6":{"i64":0},"clientId":{"i32":0},"totalAmtE6":{"i64":0},"subSrc":{"str":""}}
====begin deserial json=================
org.apache.thrift.protocol.TProtocolException: Unexpected character:p
问题:
如果fieldNamesAsString_没有设置为true,那么jasonStr变成{"1": {"i32":1111},"2":{"i32":0},"3":{"i32":0},...可以写回CreateOrderRequest,我觉得不合理。
你可以将一个对象写入一个json str,但不能读回?
【问题讨论】: