简体   繁体   English

在Java中将JSON Base64字符串转换为String

[英]Convert JSON Base64 string to String in Java

I am trying to convert a protobuf stream to JSON object using the com.google.protobuf.util.JsonFormat class as below. 我正在尝试使用com.google.protobuf.util.JsonFormat类将protobuf流转换为JSON对象,如下所示。

String jsonFormat = JsonFormat.printer().print(data);

As per the documentation https://developers.google.com/protocol-buffers/docs/proto3#json I am getting the bytes as Base64 string(example "hashedStaEthMac": "QDOMIxG+tTIRi7wlMA9yGtOoJ1g=", ). 根据文档https://developers.google.com/protocol-buffers/docs/proto3#json,我将字节作为Base64字符串获取(例如“ hashedStaEthMac”:“ QDOMIxG + tTIRi7wlMA9yGtOoJ1g =”,)。 But I would like to get this a readable string(example "locAlgorithm": "ALGORITHM_ESTIMATION", ). 但是,我想获得一个可读的字符串(例如“ locAlgorithm”:“ ALGORITHM_ESTIMATION”,)。 Below is a sample output. 以下是示例输出。 is there a way to the JSON object asplain text or any work around to get the actual values. 有没有一种方法可以将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"
  }
}

Expected format is as below. 预期格式如下。

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
}

Not easily, because the actual values are binary, which is why they're Base64-encoded in the first place. 不容易,因为实际值是二进制的,这就是为什么首先对它们进行Base64编码的原因。

Try to decode one of these values: 尝试解码以下值之一:

$ echo -n 'n5THo6IINuOSVZ/cTidNVA==' | base64 -D
??ǣ6?U??N'MT

In order to get more readable values, you have to understand what the binary data actually is, and then decide what format you want to use to display it. 为了获得更具可读性的值,您必须了解二进制数据的实际含义,然后决定要使用哪种格式来显示它。

The field called staEthMac.addr is 6 bytes and is probably an Ethernet MAC address. 名为staEthMac.addr的字段为6个字节,可能是一个以太网MAC地址。 It's usually displayed as xx:xx:xx:xx:xx:xx where xx are the hexadecimal values of each byte. 通常显示为xx:xx:xx:xx:xx:xx,其中xx是每个字节的十六进制值。 So you could decode the Base64 strings into a byte[] and then call a function to convert each byte to hex and delimit them with ':'. 因此,您可以将Base64字符串解码为byte[] ,然后调用一个函数将每个字节转换为十六进制,并用':'对其进行定界。

The fields campusId, buildingId, and floorId are 16 bytes (128 bits) and are probably UUIDs. 字段CampusId,buildingId和floorId为16个字节(128位),可能是UUID。 UUIDs are usually displayed as xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx where each x is a hex digit (4 bits). UUID通常显示为xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,其中每个x是一个十六进制数字(4位)。 So you could (again) convert the Base64 string to byte[] and then print the hex digits, optionally adding the dashes. 因此,您可以(再次)将Base64字符串转换为byte[] ,然后打印十六进制数字,还可以选择添加破折号。

Not sure about sourceId and hashedStaEthMac, but you could just follow the pattern of converting to byte[] and printing as hex. 不确定sourceId和hashedStaEthMac,但是您可以遵循转换为byte[]并以十六进制打印的模式。 Essentially you're just doing a conversion from base 64 to base 16. You'll wind up with something like this: 本质上,您只是在进行从64到16的转换。您将得到如下所示的结果:

$ echo -n 'n5THo6IINuOSVZ/cTidNVA==' | base64 -D | xxd -p
9f94c7a3a20836e392559fdc4e274d54

A point that I'm not sure you are getting is that it's binary data. 我不确定您得到的一点是二进制数据。 There is no "readable" version that makes sense like "ALGORITHM_ESTIMATION" does; 没有像“ ALGORITHM_ESTIMATION”一样有意义的“可读”版本。 the best you can do is encode the binary data using letters and numbers so you can at least pronounce it. 最好的办法是使用字母和数字对二进制数据进行编码,以便至少可以发音。

Base64 (which encodes binary using 64 different characters) is pronounceable "N five TH lowercase-O six ..." but it's not real friendly because letter case is significant and because it uses letters like O and I that look like numbers. Base64(使用64个不同的字符编码二进制)发音为“ N个5个TH小写-O 6个……”,但是它并不是真正友好的,因为字母大小写很重要,并且因为它使用像O和I这样的字母,看起来像数字。 Hex (which encodes binary using just 16 characters) is a little easier to read. 十六进制(仅使用16个字符编码二进制)更易于阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM