Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to send file, using JSON

Tags:

java

json

Is it possible to attach file to JSONObject in Java (and to JSON at all)?

For example, can I attach bitmap to 'image' field?

{'user_id':'5', 'auth_token':'abc', 'image': ???}
like image 224
Dmitry Zaytsev Avatar asked Jan 10 '12 11:01

Dmitry Zaytsev


2 Answers

You can convert the bitmap (or any binary data) to text using base64 (which makes it a String) I wouldn't use one of the Base64 classes in the JVM unless you are fully aware that they are for internal use. (may not be available on all JDKs and could change in future versions)

You could copy java.util.prefs.Base64 if you don't have one in a library already.

like image 97
Peter Lawrey Avatar answered Sep 22 '22 23:09

Peter Lawrey


refer the answer of BalusC

A bitmap is binary data. JSON is to be represented as character data. So you need to convert binary data to character data and vice versa without loss of information.

like image 39
Hemant Metalia Avatar answered Sep 23 '22 23:09

Hemant Metalia