[英]Unable to execute Java program with Json-simple jar file
I am trying to write a simple Java program which writes JSON data to a file. 我正在尝试编写一个简单的Java程序,该程序将JSON数据写入文件。 I am compiling it with the JSON simple jar file but I am getting an error.
我正在使用JSON简单jar文件进行编译,但出现错误。 Below is what I have done:
以下是我所做的:
//JsonSimpleWriteExample.java
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.io.FileWriter;
import java.io.IOException;
public class JsonSimpleWriteExample {
public static void main(String[] args) {
JSONObject obj = new JSONObject();
obj.put("name", "mkyong.com");
obj.put("age", new Integer(100));
JSONArray list = new JSONArray();
list.add("msg 1");
list.add("msg 2");
list.add("msg 3");
obj.put("messages", list);
try (FileWriter file = new FileWriter("f:\\test.json")) {
file.write(obj.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(obj);
}
}
To compile the above program in a terminal, I am doing: 要在终端中编译以上程序,我正在做:
javac -cp json-simple-1.1.1.jar JsonSimpleWriteExample.java
I downloaded the json-simple-1.1.1.jar
file from http://www.java2s.com/Code/Jar/j/json-simple.htm 我从http://www.java2s.com/Code/Jar/j/json-simple.htm下载了
json-simple-1.1.1.jar
文件
I am getting the below error: 我收到以下错误:
Note: JsonSimpleWriteExample.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
I am using MacOS. 我正在使用MacOS。 Can anybody help me figure out the issue.
谁能帮我解决这个问题。
When I try running my class file I get the below error: 当我尝试运行类文件时,出现以下错误:
$ java JsonSimpleWriteExample
Error that I got: 我得到的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject
at JsonSimpleWriteExample.main(JsonSimpleWriteExample.java:11)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONObject
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
As you have it, you compile using the external jar file, so that the compiler finds the symbols you use during compilation time. 有了它,就可以使用外部jar文件进行编译,以便编译器找到在编译期间使用的符号。
javac -cp json-simple-1.1.1.jar JsonSimpleWriteExample.java
The same has to be done at runtime: 在运行时必须执行相同的操作:
java JsonSimpleWriteExample -cp ./json-simple-1.1.1.jar
In your example, this is the output: 在您的示例中,这是输出:
{"name":"mkyong.com","messages":["msg 1","msg 2","msg 3"],"age":100}
As @Pshemo said, those "errors" you get are actually warnings. 正如@Pshemo所说,您得到的那些“错误”实际上是警告。 You can see more about unchecked or unsafe ops here and here .
您可以在此处和此处查看有关未经检查或不安全操作的更多信息。
import org.json.simple.JSONObject;
JSONObject jsonObject = (JSONObject) yourOrginalObject;
//code for get all keys from json
Set<String> keys=jsonObject.keySet();
//iterate the keys
//get values by passing keys
jsonObject.get(pass individual keys);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.