How to convert a JSONObject to a byte array and then convert that byte array to return the original JSONObject?

I am using the AWSJSONObject class . Let's say I define an object JSONObjectas follows:

JSONObject obj = new JSONObject();
obj.put("Field1": 35);

JSONObject nestedObj = new JSONObject();
nestedObj.put("Name1":"value1");
nestedObj.put("Name2":42);

obj.put("Field2": nestedObj);

So JSONObject looks like this:

{"Field1": 35,
 "Field2": {"Name1": "value1",
            "Name2": 42}
}

I want to take this one JSONObjectand somehow convert it to an array of bytes:

byte[] objAsBytes = convertToBytes(obj);

where convertToBytesis some function that does this correctly. Then I would like to take this array of bytes and convert it back to the original JSONObjectone so that it retains its original structure.

- , ? , Amazon Kinesis API PutRecord, PutRecordRequest , ByteBuffer, JSONObject ByteBuffer. , , ByteBuffer , JSONObject.

+4
1

?

byte[] objAsBytes = obj.toString().getBytes("UTF-8");

Json.simple, , , !

+4

All Articles