2016-12-01 97 views
3

我不得不產生與某些屬性JSON,其中之一是BASE64字符串:Node JS,如何在JSON字符串中正確存儲Base64字符串?

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字符串?

我生成的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 
p0NdtEsXzx5SmD70E85HPyPg7NAk7qrliy16MdJHKzN9gSDl6UDtLKNFPXvDPAjAkSPwKt1qp0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它具有'SYNJNYU/zIa // Zu3O3bp + XwH'種字符,'//'使json無效。 –

+0

@ Mr.D ['/'=(前)斜槓](https://en.wikipedia.org/wiki/Slash_(標點符號)),['\'=反斜槓](https://en.wikipedia。 org/wiki/Backslash) – RicoBrassers

回答

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+uv27AgIEAKCABIIGqHs+siCvaxABhn/GgVGEfZ/SYJN/w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VIhgA+P/xXrk8h4E10RyRi3eSdgtMaM1RAYaUADQmA1Dh5OlTtPc1JOGnMHsaBRDKYggiMBENJp6JyoU9RgS6tkhFZP0BZkCLf7t0janQSOP5gTDT2wAhgAAAAAAAAAAAAAAADA9MCEwCQYFKw4DAhoFAAQUwl/G8qYgHK0C2n1jqLTQjEjqkvkEFLPjN/qfoSjcIzyT35GhE0SgivRyAgIEAA=="} 

你擷取畫面看,它看起來像你在你的字符串有一個換行符,可能是這樣的話?我在這裏輸了:http://prnt.sc/ddyq6y

+0

'data'已經是'Buffer'了。 – felixfbecker

+0

它生成了相同的字符串,這使得這個JSON無效,並且裏面有一些'//'。我用JAVA解碼了這個字符串,它是有效的。但將它放置在JSON中,它會使這個JSON無效。 –

+0

你可以發佈字符串嗎? //不會使JSON無效 – xShirase

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:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/=",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中;

0

您可以在base64文本key: JSON.stringify(file.toString('base64'))上使用JSON.stringify。小心。注意2個的結果如下(反斜槓VS向前的斜線)的差

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"