Maitiro Ekunyora uye Kumisikidza JSON Byte Array

Iyo chaiyo nzira yekutumira mabhinari muJSON ndeye base64 kuisimbisa. Java inopa nzira dzakasiyana dzeBase64 encode uye kusarudza dheti []. Imwe yeiyi DatatypeConverter.

Ngatitii tine JSON Array sezvakanyorwa pazasi:

{
'menu': {
'id': 'file',
'value': 'File',
'popup': {

'menuitem': [


{'value': 'New', 'onclick': 'CreateNewDoc()'},


{'value': 'Open', 'onclick': 'OpenDoc()'},


{'value': 'Close', 'onclick': 'CloseDoc()'}

]
}
} }

Encode JSON se Base64

Kumisikidza iri pamusoro JSON, isu taizoshandisa

String base64Encoded = DatatypeConverter.printBase64Binary(jsonBytes);

Inoenderana:

Sarudza Base64 JSON

Kusarudza base64 encoded JSON, isu taizoshandisa

byte[] base64Decoded = DatatypeConverter.parseBase64Binary(base64Encoded);

Muenzaniso kodhi:



import javax.xml.bind.DatatypeConverter; public class JsonEncodeDecode {
public static void main(String[] args) {
String json = '{'menu': { ' +


' 'id': 'file', ' +


' 'value': 'File', ' +


' 'popup': { ' +


' 'menuitem': [ ' +


'
{'value': 'New', 'onclick': 'CreateNewDoc()'}, ' +


'
{'value': 'Open', 'onclick': 'OpenDoc()'}, ' +


'
{'value': 'Close', 'onclick': 'CloseDoc()'} ' +


' ] ' +


' } ' +


'}}';

byte[] bytes = json.getBytes();

String base64Encoded = DatatypeConverter.printBase64Binary(bytes);
System.out.println('Encoded Json: ');
System.out.println(base64Encoded + ' ');

byte[] base64Decoded = DatatypeConverter.parseBase64Binary(base64Encoded);
System.out.println('Decoded Json: ');
System.out.println(new String(base64Decoded));
} }

Kubuditsa kweiyo encoded JSON:

eyJtZW51IjogewogICJpZCI6ICJmaWxlIiwKICAidmFsdW Ongorora:Iyo encoded JSON yakaderedzwa nekuda kwehutsanana, zvikasadaro tambo refu kwazvo.

Kubuditsa kwakatemwa JSON:

{
'menu': {
'id': 'file',
'value': 'File',
'popup': {

'menuitem': [


{'value': 'New', 'onclick': 'CreateNewDoc()'},


{'value': 'Open', 'onclick': 'OpenDoc()'},


{'value': 'Close', 'onclick': 'CloseDoc()'}

]
}
} }