我不得不產生與某些屬性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
p0NdtEsXzx5SmD70E85HPyPg7NAk7qrliy16MdJHKzN9gSDl6UDtLKNFPXvDPAjAkSPwKt1qp0SEn+aZ+BADIk2TYZDTRFxGD6ym9+mwBzSsBo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ceD+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==
這裏什麼驗證說:
的base64String應該足夠,它不能有反斜槓 – xShirase
@xhirase它具有'SYNJNYU/zIa // Zu3O3bp + XwH'種字符,'//'使json無效。 –
@ Mr.D ['/'=(前)斜槓](https://en.wikipedia.org/wiki/Slash_(標點符號)),['\'=反斜槓](https://en.wikipedia。 org/wiki/Backslash) – RicoBrassers