0

I'm receiving this response from a RESTful service and saving it to a JSONObject:

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

The data above represents a binary array. I am trying to read it with the following code:

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

I would like to access the binary data and save it to a byte array, but since it does not have an attributes name I can't use:

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

How can I access the binary array?

1 Answer 1

4

Try this:

    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);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I think it throws JSONException saying that JSONArray[0] is not a number. On the following line: bArr[i] = (byte) entries.getInt(i);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.