简体   繁体   English

如何阻止 Java 将作为命令行参数传递的 JSON 字符串分离到 args[] 的单独子单元中

[英]How to stop Java from separating JSON string passed as a command line argument into separate subunits of args[]

I want to pass this JSON String to a Java class through command line arguments.我想通过命令行参数将此 JSON 字符串传递给 Java 类。

{"body": "We should definitely meet up, man", "startDate": "2014-05-29 11:00:00", "endDate": "2014-05-29 12:00:00", "location": "Boca Raton", "subject": "This is the subject of our meeting."} {"body": "我们一定要见面,伙计", "startDate": "2014-05-29 11:00:00", "endDate": "2014-05-29 12:00:00", " location": "Boca Raton", "subject": "这是我们会议的主题。"}

However, at every space, the String gets split up.但是,在每个空间中,字符串都会被拆分。 So args[0] is所以 args[0] 是

{"body": {“身体”:

args[1] is args[1] 是

"We “我们

etc.等等。

I want args[0] to just be我希望 args[0] 只是

{"body": "We should definitely meet up, man", "startDate": "2014-05-29 11:00:00", "endDate": "2014-05-29 12:00:00", "location": "Boca Raton", "subject": "This is the subject of our meeting."} {"body": "我们一定要见面,伙计", "startDate": "2014-05-29 11:00:00", "endDate": "2014-05-29 12:00:00", " location": "Boca Raton", "subject": "这是我们会议的主题。"}

I tried using double quotes, but since there are quotes in the JSON string it didn't work.我尝试使用双引号,但由于 JSON 字符串中有引号,因此它不起作用。

How can I do this?我怎样才能做到这一点? Thanks so much!非常感谢!

Here is a better solution:这是一个更好的解决方案:

  1. Stop passing json as a command line argument.停止将 json 作为命令行参数传递。
  2. Put the json in a file.将 json 放在一个文件中。
  3. Pass the name of the json file as a command line argument.将 json 文件的名称作为命令行参数传递。
  4. Read the json file in your application.读取应用程序中的 json 文件。

This isn't a Java issue: it's a shell issue.这不是 Java 问题:这是 shell 问题。 So the answer depends on what shell you're using.所以答案取决于您使用的外壳。

If you're on a UNIX-y shell, try putting the JSON within single quotes.如果您使用的是 UNIX-y shell,请尝试将 JSON 放在单引号内。 For instance: java my.MainClass '{ "key1": "value1" }' .例如: java my.MainClass '{ "key1": "value1" }' That'll probably work on Windows, too ... I'm not sure.这可能也适用于 Windows ......我不确定。

If you have a ' in your JSON, things get complicated.如果您的 JSON 中有一个' ,事情就会变得复杂。 If your shell is Bash, one option is to replace every ' with '\\'' .如果您的 shell 是 Bash,一种选择是将每个'替换为'\\''

But where is the JSON coming from?但是 JSON 来自哪里? If you're actually invoking this Java program from within another program, you can skip the shell altogether.如果您实际上是从另一个程序中调用此 Java 程序,则可以完全跳过 shell。 Python's subprocess.call , Ruby's IO.popen , Bash's "$@" and Java's ProcessBuilder all accept an array of command-line arguments instead of a single-string command. Python的subprocess.call ,Ruby的IO.popen ,bash的"$@"和Java的ProcessBuilder都接受的命令行参数,而不是一个单一字符串命令的数组。 Alternatively, logic like Ruby's Shellwords exists for just about any programming language and quotes command-line parameters so the shell parses them into exactly the bytes you specify.或者,像 Ruby 的Shellwords类的逻辑Shellwords适用于任何编程语言并引用命令行参数,以便 shell 将它们准确地解析为您指定的字节。

Another alternative: you can pipe the JSON to your program.另一种选择:您可以将 JSON 通过管道传输到您的程序。 Invoke it like this (on UNIX):像这样调用它(在 UNIX 上):

cat | java my.MainClass
{
  "you can just type": "this stuff",
  "and it will eventually get picked up by": "Java"
}
[Ctrl-d (UNIX) or Ctrl-z (Windows)]

And read it in Java as discussed at Read/convert an InputStream to a String -- using System.in as the InputStream .并在 Java 中读取它,如Read/convert an InputStream to a String - 使用System.in作为InputStream

You need to escape the double-quote characters and surround the entire String with normal double-quotes:您需要转义双引号字符并用普通双引号将整个字符串括起来:

java Test "{\"body\": \"We should definitely meet up, man\", \"startDate\": \"2014-05-29 11:00:00\", \"endDate\": \"2014-05-29 12:00:00\", \"location\": \"Boca Raton\", \"subject\": \"This is the subject of our meeting.\"}"

This is a Windows example.这是一个 Windows 示例。

Try to encode your JSON string in base64:尝试在 base64 中编码您的 JSON 字符串:

{"body": "We should definitely meet up, man", "startDate": "2014-05-29 11:00:00", "endDate": "2014-05-29 12:00:00", "location": "Boca Raton", "subject": "This is the subject of our meeting."} {"body": "我们一定要见面,伙计", "startDate": "2014-05-29 11:00:00", "endDate": "2014-05-29 12:00:00", " location": "Boca Raton", "subject": "这是我们会议的主题。"}

will be encoded to将被编码为

eyJib2R5IjogIldlIHNob3VsZCBkZWZpbml0ZWx5IG1lZXQgdXAsIG1hbiIsICJzdGFydERhdGUiOiAiMjAxNC0wNS0yOSAxMTowMDowMCIsICJlbmREYXRlIjogIjIwMTQtMDUtMjkgMTI6MDA6MDAiLCAibG9jYXRpb24iOiAiQm9jYSBSYXRvbiIsICJzdWJqZWN0IjogIlRoaXMgaXMgdGhlIHN1YmplY3Qgb2Ygb3VyIG1lZXRpbmcuIn0= eyJib2R5IjogIldlIHNob3VsZCBkZWZpbml0ZWx5IG1lZXQgdXAsIG1hbiIsICJzdGFydERhdGUiOiAiMjAxNC0wNS0yOSAxMTowMDowMCIsICJlbmREYXRlIjogIjIwMTQtMDUtMjkgMTI6MDA6MDAiLCAibG9jYXRpb24iOiAiQm9jYSBSYXRvbiIsICJzdWJqZWN0IjogIlRoaXMgaXMgdGhlIHN1YmplY3Qgb2Ygb3VyIG1lZXRpbmcuIn0 =

This eliminated spaces and other troublesome characters.这消除了空格和其他麻烦的字符。

Of course, you will need to decode it back inside your program, but this is relatively easy to do.当然,您需要将其解码回您的程序中,但这相对容易。

The shell is removing the double quotes. shell 正在删除双引号。 Instead, you could use single quotes in the json, eg相反,您可以在 json 中使用单引号,例如

{'networks':{'privateIP':'10.10.1.17','publicIP':'192.168.1.144'}}

Then parse it using:然后使用以下方法解析它:

ObjectMapper om = new ObjectMapper();
om.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
Networks n = om.readValue(args[0], Networks.class);
#!/bin/bash

JSON='
{
    "bbb":456,
    "aaaa": "SDFDS"
}
'
JSON=$(echo $JSON | sed s/\"/\\\"/g)
echo $JSON
java -Dmyparam="$JSON" Abc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM