从JSON对象获取二进制数据

时间:2013-04-03 18:55:38

标签: java android json

我从RESTful服务接收此响应并将其保存到JSONObject:

{
    "GetCampaignsInProgressListResult": [
        108,
        162,
        171,
        103,
        185,
        147,
        218,
        16,
        67,
        226,
        44,
        // etc etc
    ]
}

上面的数据代表二进制数组。我试图用以下代码阅读它:

String rawResponse = client.getResponse();
JSONArray entries = null;
JSONObject o = new JSONObject(rawResponse);
entries = new JSONArray(o.getString("GetCampaignsInProgressListResult"));

我想访问二进制数据并将其保存到字节数组中,但由于它没有属性名称,我无法使用:

for (int i = 0; i < entriesLength; i++) {
    JSONObject entry = entries.getJSONObject(i);
    Object something = entry.getString("SomeTag");
}

如何访问二进制数组?

1 个答案:

答案 0 :(得分:4)

试试这个:

    String rawResponse = client.getResponse();
    JSONArray entries = null;
    JSONObject o = new JSONObject(rawResponse);
    entries = new JSONArray(o.getString("GetCampaignsInProgressListResult"));
    byte[] bArr = new byte[entries.length()];
    for (int i = 0; i < entries.length(); i++) {
        bArr[i] = (byte) entries.getInt(i);
    }