简体   繁体   中英

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.

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=", ). But I would like to get this a readable string(example "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.

{
  "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.

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. It's usually displayed as xx:xx:xx:xx:xx:xx where xx are the hexadecimal values of each byte. 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 ':'.

The fields campusId, buildingId, and floorId are 16 bytes (128 bits) and are probably UUIDs. UUIDs are usually displayed as xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx where each x is a hex digit (4 bits). So you could (again) convert the Base64 string to byte[] and then print the hex digits, optionally adding the dashes.

Not sure about sourceId and hashedStaEthMac, but you could just follow the pattern of converting to byte[] and printing as hex. Essentially you're just doing a conversion from base 64 to base 16. You'll wind up with something like this:

$ 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; 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. Hex (which encodes binary using just 16 characters) is a little easier to read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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