2

I am creating a console app that parses JSON and does something with it. However, I am having problems preparing the user's input to be passed to the JsonParser function:

final JsonParser parser = Json.createParser(new StringReader(userInputStr));

When the console prompts the user to enter valid JSON they are most likely going to be pasting formatted json that spans multiple lines.

I am trying to figure out how I can convert all of these lines into a space-free single string that can be passed to Json.createParser().

Small samples like this work if user enters it exactly as is below:

{"name":"Falco","age":3,"bitable":false,"certificate":null}

However, the following needs to be handled prior to passing to the JsonParser:

{
   "name":"Falco",
   "age":3,
   "bitable":false,
   "certificate":null
}

I started by trying the following to process each line from the user's input:

Scanner scanner = new Scanner(new BufferedInputStream(System.in));
while (scanner.hasNext()) {
    // use scanner.next() here...
}
Blake Rivell
  • 13,105
  • 31
  • 115
  • 231

1 Answers1

-2

The problem is removing white space characters. So try jsonStr.replaceAll("\\s+","")

fakeinc
  • 33
  • 6