将字节数组放到JSON中,反之亦然

时间:2013-12-20 15:21:49

标签: java json bytearray

是否可以将byte[](字节数组)放到JSON

如果是这样,我怎么能在java中这样做?然后读取该JSON并将该字段再次转换为byte[]

6 个答案:

答案 0 :(得分:57)

这是base64编码字节数组的一个很好的例子。当您在混合中输入unicode字符以发送PDF文档之类的内容时,会变得更加复杂。对字节数组进行编码后,编码的字符串可以用作JSON属性值。

Apache commons提供了很好的实用程序:

 byte[] bytes = getByteArr();
 String base64String = Base64.encodeBase64String(bytes);
 byte[] backToBytes = Base64.decodeBase64(base64String);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding

Java服务器端示例:

public String getUnsecureContentBase64(String url)
        throws ClientProtocolException, IOException {

            //getUnsecureContent will generate some byte[]
    byte[] result = getUnsecureContent(url);

            // use apache org.apache.commons.codec.binary.Base64
            // if you're sending back as a http request result you may have to
            // org.apache.commons.httpclient.util.URIUtil.encodeQuery
    return Base64.encodeBase64String(result);
}

JavaScript解码:

//decode URL encoding if encoded before returning result
var uriEncodedString = decodeURIComponent(response);

var byteArr = base64DecToArr(uriEncodedString);

//from mozilla
function b64ToUint6 (nChr) {

  return nChr > 64 && nChr < 91 ?
      nChr - 65
    : nChr > 96 && nChr < 123 ?
      nChr - 71
    : nChr > 47 && nChr < 58 ?
      nChr + 4
    : nChr === 43 ?
      62
    : nChr === 47 ?
      63
    :
      0;

}

function base64DecToArr (sBase64, nBlocksSize) {

  var
    sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length,
    nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2, taBytes = new Uint8Array(nOutLen);

  for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
    nMod4 = nInIdx & 3;
    nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
    if (nMod4 === 3 || nInLen - nInIdx === 1) {
      for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
        taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
      }
      nUint24 = 0;

    }
  }

  return taBytes;
}

答案 1 :(得分:9)

在json中发送二进制文件的典型方法是对它进行base64编码。

Java为Base64编码和解码byte[]提供了不同的方法。其中之一是DatatypeConverter

非常简单

byte[] originalBytes = new byte[] { 1, 2, 3, 4, 5};
String base64Encoded = DatatypeConverter.printBase64Binary(originalBytes);
byte[] base64Decoded = DatatypeConverter.parseBase64Binary(base64Encoded);

您必须根据您使用的json解析器/生成器库进行此转换。

答案 2 :(得分:1)

令人惊讶的是,现在org.json现在允许您将byte []对象直接放入json中,并且仍然可读。您甚至可以通过websocket发送结果对象,并且在另一端可以读取该对象。但是我不确定结果对象的大小是大于还是小于将字节数组转换为base64的大小,如果较小则肯定会很整洁。

似乎很难衡量这样的json对象在Java中占用了多少空间。如果您的json仅由字符串组成,则只需将其字符串化即可轻松实现,但其中包含一个字节数组,我担心它不是那么简单。

用Java对json进行字符串化将我的字节数组替换为一个看起来像id的10字符串。在node.js中执行相同操作,将我们的byte[]替换为一个无引号的值<Buffered Array: f0 ff ff ...>,后者的长度表明大小会增加300%左右

答案 3 :(得分:0)

如果您的字节数组可能包含您希望看到的ASCII字符运行,则您可能更喜欢BAIS(字符串中的字节数组)格式,而不是Base64。关于BAIS的好处是,如果所有字节碰巧都是ASCII,它们会以1对1的形式转换为字符串(例如,字节数组{65,66,67}变成简单的"ABC")此外,BAIS经常会给您小于Base64的文件大小(不保证)。

将字节数组转换为BAIS字符串后,就像将其写入其他字符串一样,将其写入JSON。

这是一个Java类(从original C#移植),它将字节数组转换为字符串并返回。

import java.io.*;
import java.lang.*;
import java.util.*;

public class ByteArrayInString
{
  // Encodes a byte array to a string with BAIS encoding, which 
  // preserves runs of ASCII characters unchanged.
  //
  // For simplicity, this method's base-64 encoding always encodes groups of 
  // three bytes if possible (as four characters). This decision may 
  // unfortunately cut off the beginning of some ASCII runs.
  public static String convert(byte[] bytes) { return convert(bytes, true); }
  public static String convert(byte[] bytes, boolean allowControlChars)
  {
    StringBuilder sb = new StringBuilder();
    int i = 0;
    int b;
    while (i < bytes.length)
    {
      b = get(bytes,i++);
      if (isAscii(b, allowControlChars))
        sb.append((char)b);
      else {
        sb.append('\b');
        // Do binary encoding in groups of 3 bytes
        for (;; b = get(bytes,i++)) {
          int accum = b;
          System.out.println("i="+i);
          if (i < bytes.length) {
            b = get(bytes,i++);
            accum = (accum << 8) | b;
            if (i < bytes.length) {
              b = get(bytes,i++);
              accum = (accum << 8) | b;
              sb.append(encodeBase64Digit(accum >> 18));
              sb.append(encodeBase64Digit(accum >> 12));
              sb.append(encodeBase64Digit(accum >> 6));
              sb.append(encodeBase64Digit(accum));
              if (i >= bytes.length)
                break;
            } else {
              sb.append(encodeBase64Digit(accum >> 10));
              sb.append(encodeBase64Digit(accum >> 4));
              sb.append(encodeBase64Digit(accum << 2));
              break;
            }
          } else {
            sb.append(encodeBase64Digit(accum >> 2));
            sb.append(encodeBase64Digit(accum << 4));
            break;
          }
          if (isAscii(get(bytes,i), allowControlChars) &&
            (i+1 >= bytes.length || isAscii(get(bytes,i), allowControlChars)) &&
            (i+2 >= bytes.length || isAscii(get(bytes,i), allowControlChars))) {
            sb.append('!'); // return to ASCII mode
            break;
          }
        }
      }
    }
    return sb.toString();
  }

  // Decodes a BAIS string back to a byte array.
  public static byte[] convert(String s)
  {
    byte[] b;
    try {
      b = s.getBytes("UTF8");
    } catch(UnsupportedEncodingException e) { 
      throw new RuntimeException(e.getMessage());
    }
    for (int i = 0; i < b.length - 1; ++i) {
      if (b[i] == '\b') {
        int iOut = i++;

        for (;;) {
          int cur;
          if (i >= b.length || ((cur = get(b, i)) < 63 || cur > 126))
            throw new RuntimeException("String cannot be interpreted as a BAIS array");
          int digit = (cur - 64) & 63;
          int zeros = 16 - 6; // number of 0 bits on right side of accum
          int accum = digit << zeros;

          while (++i < b.length)
          {
            if ((cur = get(b, i)) < 63 || cur > 126)
              break;
            digit = (cur - 64) & 63;
            zeros -= 6;
            accum |= digit << zeros;
            if (zeros <= 8)
            {
              b[iOut++] = (byte)(accum >> 8);
              accum <<= 8;
              zeros += 8;
            }
          }

          if ((accum & 0xFF00) != 0 || (i < b.length && b[i] != '!'))
            throw new RuntimeException("String cannot be interpreted as BAIS array");
          i++;

          // Start taking bytes verbatim
          while (i < b.length && b[i] != '\b')
            b[iOut++] = b[i++];
          if (i >= b.length)
            return Arrays.copyOfRange(b, 0, iOut);
          i++;
        }
      }
    }
    return b;
  }

  static int get(byte[] bytes, int i) { return ((int)bytes[i]) & 0xFF; }

  public static int decodeBase64Digit(char digit)
    { return digit >= 63 && digit <= 126 ? (digit - 64) & 63 : -1; }
  public static char encodeBase64Digit(int digit)
    { return (char)((digit + 1 & 63) + 63); }
  static boolean isAscii(int b, boolean allowControlChars)
    { return b < 127 && (b >= 32 || (allowControlChars && b != '\b')); }
}

另请参阅:C# unit tests

答案 4 :(得分:0)

符合@Qwertie的建议,但更进一步,您可以假装每个字节都是ISO-8859-1字符。对于初学者来说,ISO-8859-1是一种单字节编码,与Unicode的前256个代码点匹配。

@Ash的答案实际上可以用字符集赎回:

byte[] args2 = getByteArry();
String byteStr = new String(args2, Charset.forName("ISO-8859-1"));

此编码具有与BAIS相同的可读性,其优点在于,由于需要较少的分支,因此其处理速度比BAIS或base64更快。看起来JSON解析器的功能似乎还多了一点,但这很好,因为无论如何通过转义或UTF-8处理非ASCII都是JSON解析器工作的一部分。它可以更好地映射到某些格式,例如带有配置文件的MessagePack。

但是,从空间角度讲,这通常是一种损失,因为没有人会使用UTF-16进行JSON。使用UTF-8,每个非ASCII字节将占用2个字节,而BAIS使用(2 + 4 n + r ?( r +1 ):0)字节,每次运行3 n + r 个字节(r为余数)。

答案 5 :(得分:-5)

只是这个:

byte[] args2 = getByteArry();
String byteStr = new String(args2);