我想將json轉換爲proto消息對象,但是如果在json中有一些 字符,如$或。或_我得到一個InvalidProtocolBufferException。 我原:protobuf3 JsonFormat InvalidProtocolBufferException無法識別的字符
syntax = "proto3";
package messages;
option java_multiple_files = true;
message Main {
A a = 1;
}
message A {
repeated B b = 1;
}
message B {
repeated C c = 1;
}
message C {
map<string, bytes> d = 1;
}
Java代碼:
String json = "{\n" +
" \"a\" : {\n" +
" \"b\" : [ {\n" +
" \"c\" : [ {\n" +
" \"d\" : {\n" +
" \"money\" : [ \"100$\" ]\n" +
" }\n" +
" } ]\n" +
" } ]\n" +
" }\n" +
"}";
Main.Builder builder = Main.newBuilder();
JsonFormat.parser().merge(json, builder);
System.out.println(builder.build());
輸出:
com.google.protobuf.InvalidProtocolBufferException: com.google.common.io.BaseEncoding$DecodingException: Unrecognized character: $
at com.google.protobuf.util.JsonFormat$ParserImpl.merge(JsonFormat.java:1065)
at com.google.protobuf.util.JsonFormat$Parser.merge(JsonFormat.java:273)
能否請您補充問題JSON和protobuf的定義類型用戶的? –
我已經完成了,請檢查 – idmitriev