android - parse byte[] from json using jackson/gson

Go To StackoverFlow.com

1

Can anybody help me with parsing a byte[] from a json file -

JSON File looks like this:

    {
    name:"test"
    myImage:[67,78,....]
    }

I do not want to create a new JSONObject() and parse, because this file can have multiple images and system runs out of memory.

I want to use GSON/Jackson/JSONReader(android>3.1), but it mostly seems to have getInt(), getString() objects.

How can i parse the byte[] itself into an object which i can later convert to a drawable.

I really appreciate any help.

2012-04-05 02:24
by user510164
Btw, under android 3.0, you can use GSON https://code.google.com/p/google-gson - macio.Jun 2014-02-13 21:33


0

What have you tried? In Jackson, you could use a POJO like:

class Stuff {
  public String name;
  public byte[] myImage;
}

Stuff stuff = new ObjectMapper().readValue(new File("stuff.json"), Stuff.class);

which should work.

But this JSON serialization is not the typical way to do it: it is more common to include byte array as base64 encoded data. This is how Jackson will serialize it as, if you use ObjectMapper.writeValue(...). The reason is that base64 encoding will be more compact, use less memory than number arrays.

2012-04-05 14:07
by StaxMan
I resolved it by using GSON - in this way - user510164 2012-04-07 18:15


0

I resolved it this way

InputStream instream = entity.getContent();
            Gson gson = new Gson();
            Reader reader = new InputStreamReader(instream);
            JsonReader jsonReader = new JsonReader(jsonReader);
                jsonReader.beginArray();
                while (jsonReader.hasNext()) {
                    MyCustomObject cobj = gson.fromJson(jReader,
                            MyCustomObject.class);
                    byte[] image = cobj.Image;
                    }
2012-04-07 18:18
by user510164


0

JSONObject-> getString().getBytes();

GSON/JsonReader-> getString().getBytes();

2014-02-13 22:11
by macio.Jun
Ads