Reputation: 23
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?
Upvotes: 1
Views: 2810
Reputation: 11138
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"}
If you want to download the .jar
file alternative of the JSON-Java, you can do it from here.
Upvotes: 3