英文:
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(参考实现)代替(如果您不想使用Jackson、GSON或其他更流行的库),并且这段代码:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论