Java的解析JSON字节数组 [英] Java Parse Json Byte Array

查看:1325
本文介绍了Java的解析JSON字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林开发其使用的网络服务来获取该将要显示在应用程序图像的Andr​​oid应用程序。

Im developing an Android application which are using an webservice to fetch an image that are going to be displayed in the application.

我使用JSON作为我的格式。整个连接过程就像一个魅力,但我需要分析来自Web服务请求的响应帮助。

I'm using JSON as my format. The whole connection process works like a charm but i need help with parsing the response from the webservice request.

这是我的结果:

[255,12,87,183,232,34,64,121,182,23]

当然,这不是真实的数据,我只是试图解释数据的外观,当我收到它像。所以我的问题是,我怎么分析这个数据到一个字节数组?

Of course this is not the real data, im just trying to explain how the data looks like when i receive it. So my question is, how do i parse this data into an byte array?

下面是我的连接code这一步:

Here is my connection code this far:

HttpResponse response = httpClient.execute(request);
HttpEntity responseEntity = response.getEntity();
InputStream stream = responseEntity.getContent();
String string_result= convertStreamToString(stream);

这伟大工程和变量STRING_RESULT'包含所接收到的数据。
因此,如果有人知道如何简单地分析这个JSON字符串包含字节数组我将AP preciate一些帮助。

This works great and the variable 'string_result' contains the data that was received. So if someone know how to simply parse this JSON String Containing An Byte Array i would appreciate some help.

推荐答案

下面是分析这个数组字节[] 使用不可替代的 GSON库

Here is an example of parsing this array to byte[] using the irreplaceable GSON library:

String str = "[255,12,87,183,232,34,64,121,182,23]";
Gson gson = new Gson();
byte[] parsed = gson.fromJson(str, byte[].class);
for (byte b : parsed) {
    System.out.println(b);
}

这code输出:

-1
12
87
-73
-24
34
64
121
-74
23

需要注意的是java的字节签订这样的话你得到负数。

Note that the java byte is signed and thus you get the negative numbers.

这篇关于Java的解析JSON字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆