2

Does exist an Java query string parser that support nested object ?

For example, I have the following query string : foo=bar&nested[attr]=found&nested[bar]=false

I would to a java map (Map<String, Object>) like this :

list:
  foo => bar
  nested => list:
              attr => found
              bar => false

It will be useful to generate json like this : {"foo": "bar", "nested": {"attr": "found", "bar": false}}

Aure77
  • 3,034
  • 7
  • 33
  • 53
  • Well, do you have a plain parser that gives you the key as "nested[attr]"? From there it's just a matter of iterating the map and parsing each key if it has square brackets. – RealSkeptic Sep 15 '16 at 09:12
  • Yes actually parser that I am using return "nested[attr]" as key of my map entry. I just would like to know if some library already exist or if I need to develop it. – Aure77 Sep 15 '16 at 12:02
  • Well, on StackOverflow, asking for a library is off-topic. See [help/on-topic]. So it will have to be Google or perhaps [Software Recommendations SE](http://softwarerecs.stackexchange.com/). If you don't find any and have problems in your own implementation, then StackOverflow is the right place. – RealSkeptic Sep 15 '16 at 12:07
  • This is "a specific programming problem" and I am looking for solutions (but if a library already exist, I don't want to reinvent the wheel...) – Aure77 Sep 15 '16 at 12:42
  • It is a "specific programming problem", but asking for a library is still off-topic, read the whole page. Now, knowing the community, if you just ask people for solutions to this, you'll get tons of down-votes for lack of research. The best approach would be, as I said, to look for a library on your own, and to change this question to one that presents the results of your research and asks to address further problems. – RealSkeptic Sep 15 '16 at 13:34

2 Answers2

0

Yes, there are tons of JSON parsers, the easiest one is JSONSimple, check here:

https://www.mkyong.com/java/json-simple-example-read-and-write-json/

It can handle nested objects (arrays of objects) and a lot of other things. Like you can find on the link, if you need to convert objects to/from JSON consider using something more advanced, like Jackson.

Shadov
  • 5,421
  • 2
  • 19
  • 38
  • I already have jackson to perform object to json mapping. That I am missing is a query string parser into map. – Aure77 Sep 15 '16 at 08:56
  • Oh, right, I'm sorry. Well, this link http://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection tells me that you will probably have to write something yourself. Also check this http://ostermiller.org/utils/src/CGIParser.java.html, may be helpful. Unfortunately nothing I find supports nested objects. – Shadov Sep 15 '16 at 09:03
-1

Let's write some code to encode

{
  filter: {
   make: "honda";
   model: "civic";
  }
}

into the query string filter.make=honda&filter.model=civic

const { escape } = require("querystring");

function encode(queryObj, nesting = "") {
let queryString = "";

  const pairs = Object.entries(queryObj).map(([key, val]) => {
   // Handle the nested, recursive case, where the value to encode is an object 
    itself
    if (typeof val === "object") {
     return encode(val, nesting + `${key}.`);
   } else {
     // Handle base case, where the value to encode is simply a string.
     return [nesting + key, val].map(escape).join("=");
   }
  });
  return pairs.join("&");
 }