0

I am trying to do this...

I have a big object that I need to send from the backend to the view and from that view to other backend as parameter in a GET post. So, since I cannot send all the object, I thought about make it a very small string or a number, and for this I tried doing this.

First I converted the object to JSON, using JSONObject,but due the special characters I cannot take it as a normal string (it has many ", /, {},....) so, I thought make it a byte, but I am not sure if is a correct approach, any idea how to do this?

Or something similar to have my object in an acceptable GET parameter for the URL.

So far I only have this code.

byte[] foo = new JSONObject(myObject).toString().getBytes();
String bar = foo.toString();

But dont know who to parse it back to byte and have not even tried to parse it back to JSON, any idea?

I am using spring and I dont have gson or jackson for convert to json an object.

Spencer D
  • 3,376
  • 2
  • 27
  • 43
jpganz18
  • 5,508
  • 17
  • 66
  • 115
  • 3
    The code you have given will not "work fine" - you'll end with a string such as `[B@abcdef12`. It sounds to me like you're only interested in string to `byte[]` conversions, and JSON is entirely irrelevant for that part. But "I have a json string, I want to make it a very small string (without special characters)." sounds like you want compression. Do you understand that you won't always be able to compress it? You've used `hash` in your tags, but not mentioned anything about a hash in your body... but hashing isn't reversible. It's hard to tell what you want, really... – Jon Skeet Dec 11 '15 at 18:46
  • I actually have an object, I need to send it between pages (I cannot modify so much code) so, I thought about send it as a small string or hashnumber, but the hash is not possible to get it back... Is there any way I compress my object to a small string or something like that? – jpganz18 Dec 11 '15 at 18:48
  • You probably want to read [this](http://stackoverflow.com/questions/18571223/how-to-convert-java-string-into-byte). I would typically close as duplicate, but you're involving `JSONObject`, but I don't understand why. Please edit your question to clarify it. – Sotirios Delimanolis Dec 11 '15 at 18:59
  • Thanks, Ive updated my question to make it more exact and clear – jpganz18 Dec 11 '15 at 19:11
  • Any character can be sent as a GET parameter. It just needs to be properly URL-encoded. Otherwise, you couldn't use google to search for any term containing a / or a {, for example. That said, the fact that you're sending an entire object as parameter is an indication that you should probably use a POST or a PUT rather than a GET. – JB Nizet Dec 11 '15 at 19:41
  • I wonder why the downvotes.. – jpganz18 Dec 11 '15 at 22:35

0 Answers0