在尝试将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的更多相关文章

  1. json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

    注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不 ...

  2. json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

    转:json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 执行:JSONArray arr ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. root cause:org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException

    今天在调试SSH与Ajax时,服务器端报出JSON异常:

  7. net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案

    net.sf.json.JSONException: There is a cycle in the hierarchy!错误解决方案 今天在用List集合转换成json数组的时候发生了这个错误,这个 ...

  8. 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跟个,,要不个哪的对象系列 ...

  9. net.sf.json.JSONException: There is a cycle in the hierarchy!

    因为项目中使用了AJAX技术,jar包为:json-lib.jar,在开发过程中遇到了一个JSON-LIB和Hibernate有关的问题: 如hibernate延迟加载错误,这都是些老问题了,一看就知 ...

随机推荐

  1. Android获取网页上的图片的代码

    public Bitmap getWebBitmap(String imgUrl) { Bitmap bitmap =null; try { InputStream inputStream = nul ...

  2. [golang学习] goroutine调度

    这两天有些闲功夫, 学习下golang, 确实非常简洁. 不过有些缺憾. 在我的测试中. golang的调度(goroutine)似乎不是非常好. func say(k int) { fmt.Prin ...

  3. 【原创】一些常用的Vi命令,可帮助脱离鼠标

    使用Vi编写代码时,如果想脱离鼠标,需要使用一些命令快捷键,下面罗列了一些常用的并且容易记住的: 1. 命令模式下,移动光标或跳转 0到行首 ^到行首第一个非空字符 $到行尾非空字符 fx向后移动光标 ...

  4. mac下修改mysql登录密码

    mysql版本5.7.9 在mac终端下修改mysql用户登录密码 终端命令如下: update mysql.user set authentication_string=PASSWORD(" ...

  5. React Native学习-控制横竖屏第三方组件:react-native-orientation

    在项目中,有时候可能会想使不同的页面显示的横竖屏也不一样,比如前一段我做的<广播体操>的项目,在首页面,肯定是想使页面为竖屏显示,但是播放页面要为横屏显示,即使用户的手机可以转屏,我们的播 ...

  6. date & dirname

    date 年:+%Y 月:+%m 日:+%d 时:+%H 或者 +%k 分:+%M 秒:+%S 周:+%w dirname 目录 获取目录的上级目录

  7. 转:Android中Context详解 ---- 你所不知道的Context

    转:http://blog.csdn.net/qinjuning/article/details/7310620 转:http://blog.csdn.net/lmj623565791/article ...

  8. Table of Contents - MongoDB

    Getting Started Installation Installing MongoDB on Windows Installing MongoDB on Linux Introduction ...

  9. 【CSS3】---文本阴影text-shadow

    text-shadow可以用来设置文本的阴影效果. 语法: text-shadow: X-Offset Y-Offset blur color; X-Offset:表示阴影的水平偏移距离,其值为正值时 ...

  10. Win7 服务优化个人单机版

    我的PC设备比较旧了,为了系统能流畅点,不必要的服务就不开启了.然而,服务那么多,每次重装,都要从头了解一下一边,浪费时间. 个人在网络上收集信息并结合自己的摸索,整理如下,以备查找. 服务名称  显 ...