net.sf.json.JSONException: java.lang.NoSuchMethodException
在尝试将json对象转换为list时候出现了如下错误
Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: AAA$Pdata.<init>()
at net.sf.json.JSONObject.toBean(JSONObject.java:288)
at net.sf.json.JSONArray.toCollection(JSONArray.java:444)
at net.sf.json.JSONArray.toCollection(JSONArray.java:387)
at AAA.main(SWDataCalculateFilter.java:149)
Caused by: java.lang.NoSuchMethodException: AAA$Pdata.<init>()
at java.lang.Class.getConstructor0(Class.java:2730)
at java.lang.Class.getDeclaredConstructor(Class.java:2004)
at net.sf.json.util.NewBeanInstanceStrategy$DefaultNewBeanInstanceStrategy.newInstance(NewBeanInstanceStrategy.java:55)
at net.sf.json.JSONObject.toBean(JSONObject.java:282)
... 3 more
代码如下
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class AAAAA extends BaseFilter{ public static void main(String[] args){
String json="[{\"x\":\"0\",\"y\":\"5\"},{\"x\":\"1\",\"y\":\"6\"},{\"x\":\"2\",\"y\":0}]";
JSONArray jsonarray = JSONArray.fromObject(json);
System.out.println(jsonarray);
List list = (List)JSONArray.toCollection(jsonarray, Pdata.class);
System.out.println(2.1111>2.0010);
}
//坐标内部类
public class Pdata{
public double getX() {
return x;
} public double getY() {
return y;
} public void setX(double x) {
this.x = x;
} public void setY(double y) {
this.y = y;
} private double x;
private double y;
public Pdata(){}
public Pdata(double x,double y){
this.x=x;
this.y=y;
}
} }
解决方案
1、对于目标转换类,需要添加一个参数为空的构造函数
2、对于内部类,需要加static关键字
顺利解决
net.sf.json.JSONException: java.lang.NoSuchMethodException的更多相关文章
- json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...
- json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
转:json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 执行:JSONArray arr ...
- java.sql.Date赋值给了java.util.Date.转化成JSONArray时出错net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
net.sf.json.JSONException: java.lang.reflect.InvocationTargetExceptionat net.sf.json.JSONObject.defa ...
- net.sf.json.JSONException: java.lang.reflect.InvocationTargetException Caused by: java.lang.IllegalArgumentException at java.sql.Date.getHours(Unknown Source)
数据库字段类型为Date,转成JSON格式会有问题,解决方案如下: json-lib有一个配置类JsonConfig通过JsonConfig可以注册一个字段处理器实现JsonValueProcesso ...
- java.lang.ClassNotFoundException: net.sf.json.JSONArray,java.lang.NoClassDefFoundError: net/sf/json/JSONArray jetty跑项目遇到的问题
2016-05-18 15:44:25 ERROR Dispatcher.error[user:|url:]:L38 - Dispatcher initialization failed Unable ...
- root cause:org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
今天在调试SSH与Ajax时,服务器端报出JSON异常:
- net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案
net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案 今天在用List集合转换成json数组的时候发生了这个错误,这个 ...
- atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy
atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy 1. 环境:使用hibernate4跟个,,要不个哪的对象系列 ...
- net.sf.json.JSONException: There is a cycle in the hierarchy!
因为项目中使用了AJAX技术,jar包为:json-lib.jar,在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题: 如hibernate延迟加载错误,这都是些老问题了,一看就知 ...
随机推荐
- jQuery刷新div内容,并对刷新后元素绑定事件。$(document).on()
给id=zt的元素绑定点击事件 点击刷新id=ps_list中类容(内容中含有id=zt元素) 把zt的点击事件委托到了document上,这样就不用考虑事件是否能绑定到新加元素上 代码如下: $(d ...
- WPF 之 自定义窗体标题栏
在WPF中自定义窗体标题栏,首先需要将窗体的WindowStyle属性设置为None,隐藏掉WPF窗体的自带标题栏.然后可以在窗体内部自定义一个标题栏. 例如,标题栏如下: <WrapPanel ...
- C语言第七节流程控制
流程控制 顺序结构:默认的流程结构.按照书写顺序执行每一条语句. 选择结构:对给定的条件进行判断,再根据判断结果来决定执行哪一段代码. 循环结构:在给定条件成立的情况下,反复执行某一段代码. ...
- Intervals poj 1201 差分约束系统
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22503 Accepted: 8506 Descri ...
- mysql中的longblob类型处理
longblob 对应的 C#数据类型为 byte[] 1.byte[] 与 string 之间的转换 byte[] bb = Encoding.UTF8.GetBytes(ss); string s ...
- HttpSolrServer-采用静态工厂方法,创建HttpSolrServer单实例
HttpSolrServer线程安全,如果使用下面构造器,必须对所有的请求重用相同的实例.如果实例在运行中创建的,它可能会导致连接泄漏.推荐的做法就是保持每个solr服务url的HttpSolrSer ...
- 关于 2dx v3.7 UIScale9Sprite的bug
关于 2dx v3.7 UIScale9Sprite的bug 刚把引擎从js binding v3.0升级到v3.7,发现了一些bug,这里先说说关于scale9sprite的 1. 关于capIns ...
- TCP/IP协议原理与应用笔记23:路由选择模块 和 路由表的设计
1. 路由选择模块 和 路由表的设计 2. 路由选择算法(路由模块在路由表中查找算法) (1)用IP分组中的目的IP地址查找路由表,使用匹配表项的下一跳地址完成分组交付 (2)匹配条件: dIP &a ...
- 【AsyncTask整理 2】 AsyncTask方法代码演示
Android SDK为我们提供了一个后台任务的处理工具AsyncTask.AsyncTask就是一个封装过的后台任务类顾名思义就是异步任务,方便我们维护,Android开发网提示这样的好处可以解决一 ...
- Flips测试类(page43)
测试用例:所用java类: StdOut,StdIn , Counter, StdRandom, public class Flips { public static void main(String ...