【发布时间】:2017-03-04 15:49:55
【问题描述】:
我正在尝试使用 com.google.protobuf.util.JsonFormat 类将 protobuf 流转换为 JSON 对象,如下所示。
String jsonFormat = JsonFormat.printer().print(data);
根据文档https://developers.google.com/protocol-buffers/docs/proto3#json,我将字节作为 Base64 字符串(例如“hashedStaEthMac”:“QDOMIxG+tTIRi7wlMA9yGtOoJ1g=”, )。但我想得到一个可读的字符串(例如“locAlgorithm”:“ALGORITHM_ESTIMATION”, )。下面是一个示例输出。有没有办法将 JSON 对象作为纯文本或任何解决方法来获取实际值。
{
"seq": "71811887",
"timestamp": 1488640438,
"op": "OP_UPDATE",
"topicSeq": "9023777",
"sourceId": "xxxxxxxx",
"location": {
"staEthMac": {
"addr": "xxxxxx"
},
"staLocationX": 1148.1763,
"staLocationY": 980.3377,
"errorLevel": 588,
"associated": false,
"campusId": "n5THo6IINuOSVZ/cTidNVA==",
"buildingId": "7hY/jVh9NRqqxF6gbqT7Jw==",
"floorId": "LV/ZiQRQMS2wwKiKTvYNBQ==",
"hashedStaEthMac": "xxxxxxxxxxx",
"locAlgorithm": "ALGORITHM_ESTIMATION",
"unit": "FEET"
}
}
预期格式如下。
seq: 85264233
timestamp: 1488655098
op: OP_UPDATE
topic_seq: 10955622
source_id: 00505698749E
location {
sta_eth_mac {
addr: xx:xx:xx:xx:xx:xx
}
sta_location_x: 916.003
sta_location_y: 580.115
error_level: 854
associated: false
campus_id: 9F94C7A3A20836E392559FDC4E274D54
building_id: EE163F8D587D351AAAC45EA06EA4FB27
floor_id: 83144E609EEE3A64BBD22C536A76FF5A
hashed_sta_eth_mac:
loc_algorithm: ALGORITHM_ESTIMATION
unit: FEET
}
【问题讨论】:
-
我不明白。 可读的字符串是什么意思?
-
@SotiriosDelimanolis 作为普通文本,例如“locgorithm”:“ALGORITHM_ESTIMATION”,在上述输出中与“hashedStaEthMac”相比:“QDOMIxG+tTIRi7wlMA9yGtOoJ1g=",
-
new String(Base64.getDecoder().decode(jsonFormat), StandardCharsets.UTF_8)将转换 Base64 字符串。 -
@SotiriosDelimanolis System.out.println(new String(Base64.getDecoder().decode("n5THo6IINuOSVZ/cTidNVA=="), StandardCharsets.UTF_8));正在打印 ??6?U??N'MT。使用 java.util.Base64;
标签: java json protocol-buffers