谁能帮我解决java中解析args的问题吗?
我需要通过args
以两种格式(文件、文本arg)读取JSON数据。如果 arg 是包含 JSON 文本的文件的路径,则它可以正常工作。
在主方法中我正在读取参数:
public static void main(String[] args){
String argText = args[1]
}
但是如果我将一些 JSON 文本放入 arg 中(例如: {"server1":{"dname":"www.server.com","lat":"40","lng":"17"}) 这是引号的问题,因为在
String argText
是不带引号的存储文本:
System.out.println("Text: " + argText);
Text: {server1:{dname:www.server.com,lat:40,lng:17}
以及解析JSON的方法:
public static JSONObject parseJSON(String argText){
if (text.contains("{")){
//arg is text and I want to store it as JSONObject
// how to store text with "
} else {
//argText is file....
// read file, put in new JSONObject
// it works without any problems.
}
return JsonObject
}
我不知道这是读取输入参数的好方法,但对于文件来说它正在工作,我会添加从文本中读取的内容。
如果 argText
包含转义引号 \\\"
一切都可以,但我没有这些格式的文本。
编辑:
所以我在解析 JSON 时没有问题,我需要一些方法来做到这一点:
public static void(String quotedText){
//do something...
System.out.print("Output: " + text);
}
带输入/输出:
< '"text":"val1","val2","val3"'
Output: "text":"val1","val2","val3"
在 Win system.out.print
中是:
'Output: text:val1,val2,val3'
最佳答案
引号由 shell 修改;将整个字符串放在单引号中才能正常工作。如果您的实用程序是由其他程序启动的,请修复该程序以转义所有内容,或使用标准输入传输它。
关于Java解析args中的JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23060192/