JSONObject在OpenJDK 8中受支持吗?

huangapple go评论68阅读模式
英文:

Is JSONObject supported in OpenJDK 8?

问题

我在类路径中有一个名为 json-lib-2.2-jdk15.jar 的库,但在以下代码中它的效果与预期不符:

JSONObject jsonObj = new JSONObject(); // 我将其导入为 org.json.JSONObject;
jsonObj.put("UserName", "PETER");
jsonObj.put("Age", "20");
sName = jsonObj.toString();
writeLog(logFile, "JSON 值为:" + sName + "\n");

它返回 sName = null,但我预期它应返回 sName = {"UserName":"PETER","Age":"20"}

代码有问题吗?或者 JSONObject 在 OpenJDK 8 中不起作用吗?

英文:

I have a json-lib-2.2-jdk15.jar library on my classpath, but it does not work as expected in the following code:

JSONObject jsonObj = new JSONObject(); // I import this as org.json.JSONObject;
jsonObj.put("UserName", "PETER");
jsonObj.put("Age", "20");
sName = jsonObj.toString();
writeLog(logFile, "JSON value is:" + sName + "\n");

It returns sName = null while I expect it to return sName = {"UserName":"PETER","Age":"20"}.

Is the code wrong or does JSONObject not work with OpenJDK 8?

答案1

得分: 2

Your json-lib-2.2-jdk15.jar是一个发布于2008年的版本,非常老旧,并且不再受支持。

您可以使用JSON-Java参考实现)代替(如果您不想使用JacksonGSON或其他更流行的库),并且这段代码:

JSONObject jsonObj = new JSONObject();
jsonObj.put("UserName", "PETER");
jsonObj.put("Age", "20");
String sName = jsonObj.toString();
System.out.println(sName);

将输出:

{"UserName":"PETER","Age":"20"}

如果您想要下载JSON-Java的.jar文件,您可以从这里下载。

英文:

Your json-lib-2.2-jdk15.jar is a release of 2008, which is very old, and which is no longer supported.

You can use JSON-Java (reference implementation) instead (if you do not want to use Jackson, GSON or something more popular), and this code:

    JSONObject jsonObj = new JSONObject();
    jsonObj.put("UserName", "PETER");
    jsonObj.put("Age", "20");
    String sName = jsonObj.toString();
    System.out.println(sName);

will output:

{"UserName":"PETER","Age":"20"}

<sub>If you want to download the .jar file alternative of the JSON-Java, you can do it from here.</sub>

huangapple
  • 本文由 发表于 2020年8月21日 15:54:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63518778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定