Java 将 byte[] 转换为 JsonObject
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36912389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:48:24 来源:igfitidea点击:
Converting byte[] to JsonObject
提问by Tolgay Toklar
I want to convert byte
to JsonObject
. I tried like this:
我想转换byte
为JsonObject
. 我试过这样:
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?
我怎样才能做到这一点?
采纳答案by Guillaume Barré
Try this :
尝试这个 :
String testV=new JSONObject(new String(responseBody)).toString();
or this if you need a JSONObject
或者如果你需要一个 JSONObject
JSONObject testV=new JSONObject(new String(responseBody));
The issue is that you declare a String
variable and intent to store a JSONObject
into it.
问题在于您声明了一个String
变量并打算将 a 存储JSONObject
到其中。
回答by jamesomahony
JSONArray testV = new JSONArray(new String(responseBody));
回答by Anksss
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
JSONObject jsonObject = new JSONObject(IOUtils.toString(responseBody, StandardCharsets.UTF_8));