1

Given a query string that contains simple types (string, date), arrays, and complex objects, how can I easily create the JSON represenation using Java?

For instance:

type=event&groups%5B%5D=a&groups%5B%5D=b&details%5Bclient_time%5D=Sat+Jan+30+2016+18%3A38%3A57+GMT-0500+(EST)

should produce:

{ type: 'event', groups: [ 'a', 'b' ], details: { client_time: 'Sat Jan 30 2016 18:38:57 GMT-0500 (EST)' } }

Such functionality exists in the Node.js Express framework (the result is readily available as request.query).

4
  • That string translates to this type=event&groups[]=a&groups[]=b&details[client_time]=Sat Jan 30 2016 18:38:57 GMT-0500 (EST) Does that look right? Because I think it should be this: type%3Devent%26groups%3D%5B%27a%27%2C%27b%27%5D%26+details%3Dclient_time%3D%27Sat+Jan+30+2016+18%3A38%3A57+GMT-0500+%28EST%29%27 Commented Jan 31, 2016 at 5:40
  • No, Correct JSON serialization of this value is represented in the answer after 'should produce:' Commented Jan 31, 2016 at 5:44
  • I understand that the serialized version after is correct. I'm asking if the original string is correct. Because groups%5B%5D=a results in groups[]=a... Commented Jan 31, 2016 at 5:48
  • Sorry - I misunderstood. I do believe this correct, since this is query string the result of calling jQuery.param() on a valid JSON object. From jQuery documentation: : Create a serialized representation of an array, a plain object, or a jQuery object suitable for use in a URL query string or Ajax request. In case a jQuery object is passed, it should contain input elements with name/value properties. Commented Jan 31, 2016 at 5:51

1 Answer 1

3

Im still unsure (per the comments on the question) that the original string is correctly formatted. I think some chars got moved around when you where replacing values with dummy data. I don't think there is a logical transition from your original string to the serialized string

Take a look at this

final String opString = "type=event&groups%5B%5D=a&groups%5B%5D=b&details%5Bclient_time%5D=Sat+Jan+30+2016+18%3A38%3A57+GMT-0500+(EST)";

System.out.println(URLDecoder.decode(opString, "UTF-8"));

which outputs

type=event&groups[]=a&groups[]=b&details[client_time]=Sat Jan 30 2016 18:38:57 GMT-0500 (EST)

Where is my interpretation of your string

final String  myString = "type%3Devent%26groups%3D%5B%27a%27%2C%27b%27%5D%26details%5Bclient_time%5D=Sat+Jan+30+2016+18%3A38%3A57+GMT-0500+(EST)";
    System.out.println(URLDecoder.decode(myString, "UTF-8"));

which output

type=event&groups=['a','b']&details[client_time]=Sat Jan 30 2016 18:38:57 GMT-0500 (EST)

I can logically work with that to produce the string you want

    final String myStringDecoded = URLDecoder.decode(myString, "UTF-8");

    System.out.println(myStringDecoded);

    // Then we can break it down to its parts
    // The & is used to operate values
    String[] parts = myStringDecoded.split("&");


    JsonObject json = new JsonObject();

    for(String part: parts){
      String[] keyVal = part.split("="); // The equal separates key and values
        json.addProperty(keyVal[0], keyVal[1]);
    }

    System.out.println(json);

which results in

{"type":"event","groups":"['a','b']","details[client_time]":"Sat Jan 30 2016 18:38:57 GMT-0500 (EST)"}

I can improve on this answer to make it exactly what you want if my assumptions are correct

Sign up to request clarification or add additional context in comments.

2 Comments

Crito - thanks for your reply. Again, I do not believe that the query string example is incorrect or ortherwise modified. Use this JsFiddle link as an example of how this VALID json object is converted into the above query string: jsfiddle.net/z3akqmew Suggesting that the above query string is invalid would be to suggest that jQuery is incorrectly serializing JSON objects into query strings. That is very, very, bold claim.
Honestly, I don't know what to say. type=event&groups[]=a&groups[]=b&details[client_time]=Sat Jan 30 2016 18:38:57 GMT-0500 (EST) makes no logical sense. So I think there is something wrong with that lib maybe? Can you try writing code to get the JSON back in javascript? This is what I got when using just pure javascript which is consistent with what I have in my answer.

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.