当我尝试实例化JSONObject时出现错误:
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'budgetController' defined in file [C:\Users\yqb18196\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Budget\WEB-INF\classes\biz\martyn\budget\controller\BudgetController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [biz.martyn.budget.controller.BudgetController]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/json/JSONException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
...
在这里发生错误:
public class ApiClient {
...
public void requestData() {
String str = "{}";
JSONObject obj = new JSONObject(str); // <-- error
// ...
}
}
如果重要的话,可以从Spring MVC控制器中调用它:
@Controller
public class BudgetController {
public BudgetController() {
ApiClient apiClient = new ApiClient();
apiClient.requestData();
}
...
我看了无数有关如何使用JSONObject的教程,但我无法确切地知道我是如何错误地使用它的,但是后来我还是Java的新手,所以可能还有别的东西。