Java 无法在.json文件中正确写入链接
我在WebSphere 8.5上部署了一个使用java 1.6的动态Web Project+Maven,我正在pom.xml中使用以下库:Java 无法在.json文件中正确写入链接,java,websphere,Java,Websphere,我在WebSphere 8.5上部署了一个使用java 1.6的动态Web Project+Maven,我正在pom.xml中使用以下库: <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</versi
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
好吧,它是有效的,但我在conf.json上得到了这样的信息:
{"name":"https:\/\/126.0.0.0:443\/authorizer\/","debugging":"true"}
正如您所看到的,它没有格式化,但当我在Windows上测试它时,它运行良好。
问题是,当我在WebSphere上部署时,我得到了错误的url,而问题是它在正斜杠上。如何正确地写入url
我已经尝试过使用
File.separator
,或者简单地编写“/”
或“\\”
,但都不起作用。不要使用File.separator构建URL,只需使用/
。URL的路径分隔符是相同的,与操作系统无关。File.separator因操作系统而异(Windows是\
,大多数其他操作系统是/
)。我重写了部分代码来演示URL的构造。请注意,当您从Json对象获取URL时,它的格式是正确的(请参阅以下行:System.out.println(“从Json恢复的URL:…
)。该行的输出如下所示:
从Json恢复的URL:https://127.0.1.1:443/authorizer/
public static void main(String... args) throws Exception {
InetAddress ip;
ip = InetAddress.getLocalHost();
ip.getHostAddress();
String url0 = "https:" + File.separatorChar + File.separatorChar + ip.getHostAddress() + ":443"
+ File.separatorChar + "authorizer" + File.separatorChar;
String url1 = "https:" + "//" + ip.getHostAddress() + ":443"
+ "/" + "authorizer" + "/";
JSONObject obj = new JSONObject();
obj.put("name", url0);
obj.put("debugging", "true");
System.out.println("the URL recovered from Json: " + obj.get("name"));
System.out.println("json: " + obj.toJSONString());
System.out.println("this url will be different than what you want on Windows:\n" + url0);
System.out.println("this is what you want, it will be the same on all OSs:\n" + url1);
}
Json似乎允许转义
/
,但并不需要转义。因此,转义/
并不是不正确的,只是在您的情况下是不必要的。请参阅:因为您试图构建一个URL,而不是文件系统中文件的路径,所以您不应该使用文件。分隔符,您应该简单地使用“/“.On Windows OSs File.separator是一个\,在基于unix的OSs上是一个\。尤其是在Windows上,你不希望URL有\。我得到了这个{“name”:“https:\/126.0.0.0:443\/authorizer\/”,“debuging”:“true”}
public static void main(String... args) throws Exception {
InetAddress ip;
ip = InetAddress.getLocalHost();
ip.getHostAddress();
String url0 = "https:" + File.separatorChar + File.separatorChar + ip.getHostAddress() + ":443"
+ File.separatorChar + "authorizer" + File.separatorChar;
String url1 = "https:" + "//" + ip.getHostAddress() + ":443"
+ "/" + "authorizer" + "/";
JSONObject obj = new JSONObject();
obj.put("name", url0);
obj.put("debugging", "true");
System.out.println("the URL recovered from Json: " + obj.get("name"));
System.out.println("json: " + obj.toJSONString());
System.out.println("this url will be different than what you want on Windows:\n" + url0);
System.out.println("this is what you want, it will be the same on all OSs:\n" + url1);
}