Node JS,如何在JSON字符串中正确存储Base64字符串?

时间:2016-12-01 08:48:38

标签: javascript json node.js string

我必须使用一些属性生成JSON,其中一个是base64 String:

var file = fs.readFileSync('myfile');
var request = { password: '123456', key: file.toString('base64')};
console.log(JSON.stringify(request));

它向我输出那种JSON:

{"password":"123456","key":"MIIIhQIBAzCCCD8GCSqGSIb3DQEHAaCCCDAEgggsMIAwgAYJKoZIhvcNA
  QcBoIAEggEOMIIBCjCCAQYGCyqGSIb3DQEMCgECoG4wbDAoBgoqhkiG9w0BDAEDMBoEFOWB9Rf4cT/NBObHUQW6sCOtB2AbAgIEAARAfTkFJwD66tZ2p30iXGLNtyH41D4RlWSof/MJ3w91rU22sQmCZkRR7UzgOlaoFWggWB2a2FHYOMpNjRbm+d
  9+hjGBhjAjBgkqhkiG9w0BCRUxFgQU5W/loImfeHgVra9dJW2noKcMLBMwXwYJKoZIhvcNAQkUMVIeUABlADUANgBmAGUANQBhADAAOAA5ADkAZgA3ADgANwA4ADEANQBhAGQAYQBmADUAZAAyADUANgBkAGEANwBhADAAYQA3ADAAYwAyAGMAMQA
  zAAAAADCABgkqhkiG9w0BBwaggDCAAgEAMIAGCSqGSIb3DQEHATAoBgoqhkiG9w0BDAEGMBoEFPOj+9YztiaevvtCfZthUx6+uv27AgIEAKCABIIGqHs+siCvaxABhn/GgVGEfZ/SYJN/wBszCjJDpyiv1dO1c3Ty3yPzxzqCwpvYvZcXLpzc6Xa6
  v90tmrml63zIOwPyDSKqZ0nSC8fL+jqMRXArUYlDoP+TK39OknfPEhhjLmJOqY2jboRvsDXaOggu0g7VOhx4vG6e/nj8LjFgEQEzoqF5wxg/RTx5yNvnK3oUPocbKUjSgJ1qj26vJUk3vXNTGjks9ltswRZA5up+b+3bzx5Cy/gCU/U96bw3u+8Ei
  p0NdtEsXzx5SmD70E85HPyPg7NAk7qrliy16MdJHKzN9gSDl6UDtLKNFPXvDPAjAkSPwKt1qp0SEn+aZ+BADIk2TYZDTRFxGD6ym9+mwBzSsBoJ5kojmyF3F2CJCQzjyEs2moCCV32sgHJ59k3C1DOGD4b2TNxRKm4U0hjpdXqjv2orPDpkvniejJ
"}

我刚刚在这个问题中跳过了base64字符串的最后几部分。

它为我生成了JSON字符串并且它无效,因为base64字符串在其结构中有一些双反斜杠。

如何使用base64属性正确生成JSON字符串?

My Generated base64如下所示:

MIIIhQIBAzCCCD8GCSqGSIb3DQEHAaCCCDAEgggsMIAwgAYJKoZIhvcNA
QcBoIAEggEOMIIBCjCCAQYGCyqGSIb3DQEMCgECoG4wbDAoBgoqhkiG9w0BDAEDMBoEFOWB9Rf4cT/NBObHUQW6sCOtB2AbAgIEAARAfTkFJwD66tZ2p30iXGLNtyH41D4RlWSof/MJ3w91rU22sQmCZkRR7UzgOlaoFWggWB2a2FHYOMpNjRbm+d
9+hjGBhjAjBgkqhkiG9w0BCRUxFgQU5W/loImfeHgVra9dJW2noKcMLBMwXwYJKoZIhvcNAQkUMVIeUABlADUANgBmAGUANQBhADAAOAA5ADkAZgA3ADgANwA4ADEANQBhAGQAYQBmADUAZAAyADUANgBkAGEANwBhADAAYQA3ADAAYwAyAGMAMQA
zAAAAADCABgkqhkiG9w0BBwaggDCAAgEAMIAGCSqGSIb3DQEHATAoBgoqhkiG9w0BDAEGMBoEFPOj+9YztiaevvtCfZthUx6+uv27AgIEAKCABIIGqHs+siCvaxABhn/GgVGEfZ/SYJN/wBszCjJDpyiv1dO1c3Ty3yPzxzqCwpvYvZcXLpzc6Xa6
v90tmrml63zIOwPyDSKqZ0nSC8fL+jqMRXArUYlDoP+TK39OknfPEhhjLmJOqY2jboRvsDXaOggu0g7VOhx4vG6e/nj8LjFgEQEzoqF5wxg/RTx5yNvnK3oUPocbKUjSgJ1qj26vJUk3vXNTGjks9ltswRZA5up+b+3bzx5Cy/gCU/U96bw3u+8Ei
p0NdtEsXzx5SmD70E85HPyPg7NAk7qrliy16MdJHKzN9gSDl6UDtLKNFPXvDPAjAkSPwKt1qp0SEn+aZ+BADIk2TYZD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bAdUk/pIgLQFQ9/FDmceD+b82hxCYG6O2cx9ppCxbxChbiQmxUi0jW8dUakWrXkEjcz/Aytvon/STMuzkh6SLGVW0hr4NbA0rPFwAl/d9Fi/NsVOlqq4D3FZ4q76rgRGQkEcmMESSF051DdDFO+cYAaA2
MwkIr7dKccvdXyLbKkL/0u23lgWECRXg4c4EfqZMticDtP7letIc7yeNgAEglbLvvRqPoRtAiCZg7yzGUGnjT9qZRnujmZrGKDKXDYry8oBqRKUJEOikHmGmEEw9Hs9hyFos7AzBP6t/93PI/kUaPdRhZe9Fwi9AwnrIfB8jxAfNPEy2nLVZ6WEQm
tHJD8LAGVsiUtlQA30C/YAoX284RlsfHTG9Cw2S0u3lSYNJNYU/zIa//Zu3O3bp+XwHUec7al9QcofiRToZwC/YA9mZEGDOqjP8gh4Q0WFONTRTSD6LIZem8x8Y+fEWcPDu4ZzkExZy8zC9KYletJglM/ZDcbQd8vhiytpTODuFYSXJIsLmk5wYkT
0Gdb3QiRD6tdZ+GOcHT0oaSHEkWGLa7hfSIdaS5j4G+VAPy+29CLCphX9ZdRyuae4C2dVjoHcu2sFhhCQh7V64mYpwp2AgIlrx5T5+GKmjnsi8MhXnMUY8C/tAYY0JQwsEsArE04FOqJerWvhM/cB7CvdQN6NyG0XiV4mT6LDn1Vz1u8nuXB+xKvT
nVNl7MfJtxLuEe6IC0jttX2QwbtlSHTYKHA1+4lKe9SKbtWV1VIhgA+P/xXrk8h4E10RyRi3eSdgtMaM1RAYaUADQmA1Dh5OlTtPc1JOGnMHsaBRDKYggiMBENJp6JyoU9RgS6tkhFZP0BZkCLf7t0janQSOP5gTDT2wAhgAAAAAAAAAAAAAAADA9
MCEwCQYFKw4DAhoFAAQUwl/G8qYgHK0C2n1jqLTQjEjqkvkEFLPjN/qfoSjcIzyT35GhE0SgivRyAgIEAA==

这里有什么验证者说:

json

4 个答案:

答案 0 :(得分:0)

使用btoa(string)为我工作:

var string = 'Some string';

// Encoding
var encodedString = btoa(string);
console.log(encodedString); // out: "SGVsbG8gV29ybGQh"

// Decoding
var decodedString = atob(encodedString);
console.log(decodedString); // out: "Some string"

有些案例可以像这样使用:

var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){var t="";var n,r,i,s,o,u,a;var f=0;e=Base64._utf8_encode(e);while(f<e.length){n=e.charCodeAt(f++);r=e.charCodeAt(f++);i=e.charCodeAt(f++);s=n>>2;o=(n&3)<<4|r>>4;u=(r&15)<<2|i>>6;a=i&63;if(isNaN(r)){u=a=64}else if(isNaN(i)){a=64}t=t+this._keyStr.charAt(s)+this._keyStr.charAt(o)+this._keyStr.charAt(u)+this._keyStr.charAt(a)}return t},decode:function(e){var t="";var n,r,i;var s,o,u,a;var f=0;e=e.replace(/[^A-Za-z0-9+/=]/g,"");while(f<e.length){s=this._keyStr.indexOf(e.charAt(f++));o=this._keyStr.indexOf(e.charAt(f++));u=this._keyStr.indexOf(e.charAt(f++));a=this._keyStr.indexOf(e.charAt(f++));n=s<<2|o>>4;r=(o&15)<<4|u>>2;i=(u&3)<<6|a;t=t+String.fromCharCode(n);if(u!=64){t=t+String.fromCharCode(r)}if(a!=64){t=t+String.fromCharCode(i)}}t=Base64._utf8_decode(t);return t},_utf8_encode:function(e){e=e.replace(/rn/g,"n");var t="";for(var n=0;n<e.length;n++){var r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r)}else if(r>127&&r<2048){t+=String.fromCharCode(r>>6|192);t+=String.fromCharCode(r&63|128)}else{t+=String.fromCharCode(r>>12|224);t+=String.fromCharCode(r>>6&63|128);t+=String.fromCharCode(r&63|128)}}return t},_utf8_decode:function(e){var t="";var n=0;var r=c1=c2=0;while(n<e.length){r=e.charCodeAt(n);if(r<128){t+=String.fromCharCode(r);n++}else if(r>191&&r<224){c2=e.charCodeAt(n+1);t+=String.fromCharCode((r&31)<<6|c2&63);n+=2}else{c2=e.charCodeAt(n+1);c3=e.charCodeAt(n+2);t+=String.fromCharCode((r&15)<<12|(c2&63)<<6|c3&63);n+=3}}return t}}

// String
var string = 'Some string';

// Encoding
var encodedString = Base64.encode(string);
console.log(encodedString); // out: "SGVsbG8gV29ybGQh"

// Decoding
var decodedString = Base64.decode(encodedString);
console.log(decodedString); // Outputs: "some string"

然后你可以将你的字符串存储到你的Json中;

答案 1 :(得分:0)

你的方法很好,Base64字符串不能包含反斜杠,其余字符串将被转义。

Base64 index Table :

0   A   16  Q   32  g   48  w
1   B   17  R   33  h   49  x
2   C   18  S   34  i   50  y
3   D   19  T   35  j   51  z
4   E   20  U   36  k   52  0
5   F   21  V   37  l   53  1
6   G   22  W   38  m   54  2
7   H   23  X   39  n   55  3
8   I   24  Y   40  o   56  4
9   J   25  Z   41  p   57  5
10  K   26  a   42  q   58  6
11  L   27  b   43  r   59  7
12  M   28  c   44  s   60  8
13  N   29  d   45  t   61  9
14  O   30  e   46  u   62  +
15  P   31  f   47  v   63  /

并且使用给定的字符串,它可以完美地运行:

a={"password":"123456",key:"MIIIhQIBAzCCCD8GCSqGSIb3DQEHAaCCCDAEgggsMIAwgAYJKoZIhvcNAQcBoIAEggEOMIIBCjCCAQYGCyqGSIb3DQEMCgECoG4wbDAoBgoqhkiG9w0BDAEDMBoEFOWB9Rf4cT/NBObHUQW6sCOtB2AbAgIEAARAfTkFJwD66tZ2p30iXGLNtyH41D4RlWSof/MJ3w91rU22sQmCZkRR7UzgOlaoFWggWB2a2FHYOMpNjRbm+d9+hjGBhjAjBgkqhkiG9w0BCRUxFgQU5W/loImfeHgVra9dJW2noKcMLBMwXwYJKoZIhvcNAQkUMVIeUABlADUANgBmAGUANQBhADAAOAA5ADkAZgA3ADgANwA4ADEANQBhAGQAYQBmADUAZAAyADUANgBkAGEANwBhADAAYQA3ADAAYwAyAGMAMQAzAAAAADCABgkqhkiG9w0BBwaggDCAAgEAMIAGCSqGSIb3DQEHATAoBgoqhkiG9w0BDAEGMBoEFPOj+9YztiaevvtCfZthUx6+uv27AgI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Ee6IC0jttX2QwbtlSHTYKHA1+4lKe9SKbtWV1VIhgA+P/xXrk8h4E10RyRi3eSdgtMaM1RAYaUADQmA1Dh5OlTtPc1JOGnMHsaBRDKYggiMBENJp6JyoU9RgS6tkhFZP0BZkCLf7t0janQSOP5gTDT2wAhgAAAAAAAAAAAAAAADA9MCEwCQYFKw4DAhoFAAQUwl/G8qYgHK0C2n1jqLTQjEjqkvkEFLPjN/qfoSjcIzyT35GhE0SgivRyAgIEAA=="}

查看你的screencap,看起来你的字符串中有换行符,可能就是这种情况吗?我在这里不知所措:http://prnt.sc/ddyq6y

答案 2 :(得分:0)

您可以在base64文本key: JSON.stringify(file.toString('base64'))上使用JSON.stringify。小心。请注意下面2个结果的差异(反斜杠与正斜杠)

var s="SYNJNYU/zIa//Zu3O3bp+XwH"; console.log(JSON.stringify(s));

输出

"SYNJNYU/zIa//Zu3O3bp+XwH"

但是

var s="SYNJNYU\zIa\\Zu3O3bp+XwH"; console.log(JSON.stringify(s));

输出

"SYNJNYUzIa\\Zu3O3bp+XwH"

答案 3 :(得分:0)

是的,btoa和atob可以完美工作,但是,如果您要美化或必须在JSON字段中添加一些格式,则可以使用。 JSON.stringify(jsonObject,undefined,2)。我在这里添加2的空间。

let alpha = {"name":"AWS","location":"Singapore","Region":"Asia-Pacific"}

//here it'll give your code a nice formatting ie space of 2
let encodedWithSpace = window.btoa(JSON.stringify(alpha,undefined,2));
//No space given here
let encodedWOSpace = window.btoa(JSON.stringify(alpha));

let decodedWithSpace = window.atob(encodedWithSpace);
let decodedWOSpace = window.atob(encodedWOSpace);
console.log("Decoded with Space of 2 \n", decodedWithSpace)
console.log("Decoded without space \n", decodedWOSpace)