Reputation: 606
I have Base64 Encoded string and want to convert it to JSON object.
Here is encoded String
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQU...
Here is how i am doing.
String json = {"image": encode_string};
try{
JSONObject obj = new JSONObject(json);
Log.d("My App", obj.toString());
}catch (Throwable t){
t.printStackTrace();
}
But when i write this line String json = {"image":encode_string};
i got compile time error.
Unexpected Token
How to resolve that Thanks in advance.
Upvotes: 0
Views: 1407
Reputation: 191983
You could make the JSONObject like a HashMap instead of having it parse a string.
This also removes the need for the try-catch.
JSONObject obj = new JSONObject();
obj.put("image", encode_string);
Upvotes: 1
Reputation: 562
String json = {"image": encode_string};
The right side of this equation doesn't return string value. Instead what you should do is:
String json = "{" + "\"image\":" + encode_string + "}";
Upvotes: 0