Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting byte[] to JsonObject

Tags:

java

android

I want to convert byte to JsonObject. I tried like this:

public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
    String testV=new JsonObject(new String(responseBody));
} 

But I am getting compiler error:

JsonObject cannot be applied to java.lang.String

How can I do this?

like image 679
Tolgay Toklar Avatar asked Apr 28 '16 10:04

Tolgay Toklar


People also ask

Can I send byte array in JSON?

JSON does not support that. Use Base64. That is your library supporting it, not JSON itself. The byte array wont be stored as byte array in the JSON, JSON is a text format meant to be human readable.

Can a JSONObject be an array?

JSON can be either an array or an object.


1 Answers

Try this :

String testV=new JSONObject(new String(responseBody)).toString();

or this if you need a JSONObject

JSONObject testV=new JSONObject(new String(responseBody));

The issue is that you declare a String variable and intent to store a JSONObjectinto it.

like image 130
Guillaume Barré Avatar answered Sep 28 '22 01:09

Guillaume Barré