尝试使用google的simple-json解析一个简单的json时,我遇到了奇怪的问题。
这是我的代码无效:
String s = args[0].toString();
JSONObject json = (JSONObject)new JSONParser().parse(s);
当我执行时,它会给我异常java.lang.String cannot be cast to org.json.simple.JSONObject
但是当我直接硬编码json时,它的工作正常。 Wat可能是原因吗?
JSONObject json = (JSONObject)new JSONParser().parse("{\"application\":\"admin\",\"keytype\":\"PRODUCTION\",\"callbackUrl\":\"qwerewqr;ewqrwerq;qwerqwerq\",\"authorizedDomains\":\"ALL\",\"validityTime\":\"3600000\",\"retryAfterFailure\":true}");
更新
当我打印s时,它会给我输出如下:
"{\"application\":\"admin\",\"keytype\":\"PRODUCTION\",\"callbackUrl\":\"qwerewqr;ewqrwerq;qwerqwerq\",\"authorizedDomains\":\"ALL\",\"validityTime\":\"3600000\",\"retryAfterFailure\":true}"
答案 0 :(得分:6)
我在arguments
中提供run configuration
,通过eclipse运行。
public static void main(String[] args) {
String s = args[0].toString();
System.out.println("=>" + s);
try {
JSONObject json = (JSONObject) new JSONParser().parse(s);
System.out.println(json);
} catch (ParseException e) {
e.printStackTrace();
}
}
<强>输出强>
=>{"application":"admin","keytype":"PRODUCTION","callbackUrl":"qwerewqr;ewqrwerq;qwerqwerq","authorizedDomains":"ALL","validityTime":"3600000","retryAfterFailure":true}
{"validityTime":"3600000","callbackUrl":"qwerewqr;ewqrwerq;qwerqwerq","application":"admin","retryAfterFailure":true,"authorizedDomains":"ALL","keytype":"PRODUCTION"}
答案 1 :(得分:2)
确保字符串是有效的 JSON。您可以使用带有给定字符串的 JSONObject 参数化构造函数将 JSON 字符串转换为有效的 JSON 对象。
例如
String jsonString = "{'application':'admin','keytype':'PRODUCTION','callbackUrl':'qwerewqr;ewqrwerq;qwerqwerq','authorizedDomains':'ALL','validityTime':3600000,'retryAfterFailure':true}";
JSONObject data = new JSONObject(jsonString);
String application = data.getString("application "); //gives admin
String keytype = data.getString("keytype"); //gives PRODUCTION
答案 2 :(得分:1)
我遇到了同样的问题
package com.test;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JSONTest {
public static void main(String[] args) {
String s = args[0];
try {
JSONObject json = new JSONObject((String) new JSONParser().parse(s));
System.out.println(json);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
这对我有用
答案 3 :(得分:0)
试试这个
package com.test;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JSONTest {
public static void main(String[] args) {
String s = args[0];
try {
JSONObject json = (JSONObject) new JSONParser().parse(s);
System.out.println(json);
} catch (ParseException e) {
e.printStackTrace();
}
}
然后在命令行
java -classpath ".;json-simple-1.1.1.jar" com.test.JSONTest {\"application\":\"admin\",\"keytype\":\"PRODUCTION\",\"callbackUrl\":\"qwerewqr;ewqrwerq;qwerqwerq\",\"authorizedDomains\":\"ALL\",\"validityTime\":\"3600000\",\"retryAfterFailure\":true}
输出是
{"validityTime":"3600000","callbackUrl":"qwerewqr;ewqrwerq;qwerqwerq","application":"admin","retryAfterFailure":true,"authorizedDomains":"ALL","keytype":"PRODUCTION"}