在尝试将json对象转换为list时候出现了如下错误

  1. Exception in thread "main" net.sf.json.JSONException: java.lang.NoSuchMethodException: AAA$Pdata.<init>()
  2. at net.sf.json.JSONObject.toBean(JSONObject.java:288)
  3. at net.sf.json.JSONArray.toCollection(JSONArray.java:444)
  4. at net.sf.json.JSONArray.toCollection(JSONArray.java:387)
  5. at AAA.main(SWDataCalculateFilter.java:149)
  6. Caused by: java.lang.NoSuchMethodException: AAA$Pdata.<init>()
  7. at java.lang.Class.getConstructor0(Class.java:2730)
  8. at java.lang.Class.getDeclaredConstructor(Class.java:2004)
  9. at net.sf.json.util.NewBeanInstanceStrategy$DefaultNewBeanInstanceStrategy.newInstance(NewBeanInstanceStrategy.java:55)
  10. at net.sf.json.JSONObject.toBean(JSONObject.java:282)
  11. ... 3 more

代码如下

  1. import java.text.DecimalFormat;
  2. import java.util.Date;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Vector;
  7.  
  8. import net.sf.json.JSONArray;
  9. import net.sf.json.JSONObject;
  10.  
  11. public class AAAAA extends BaseFilter{
  12.  
  13. public static void main(String[] args){
  14. String json="[{\"x\":\"0\",\"y\":\"5\"},{\"x\":\"1\",\"y\":\"6\"},{\"x\":\"2\",\"y\":0}]";
  15. JSONArray jsonarray = JSONArray.fromObject(json);
  16. System.out.println(jsonarray);
  17. List list = (List)JSONArray.toCollection(jsonarray, Pdata.class);
  18. System.out.println(2.1111>2.0010);
  19. }
  20. //坐标内部类
  21. public class Pdata{
  22. public double getX() {
  23. return x;
  24. }
  25.  
  26. public double getY() {
  27. return y;
  28. }
  29.  
  30. public void setX(double x) {
  31. this.x = x;
  32. }
  33.  
  34. public void setY(double y) {
  35. this.y = y;
  36. }
  37.  
  38. private double x;
  39. private double y;
  40. public Pdata(){}
  41. public Pdata(double x,double y){
  42. this.x=x;
  43. this.y=y;
  44. }
  45. }
  46.  
  47. }

解决方案

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. solr返回的字段带有中括号问题

    在solr返回的结果集的content字段返回格式为: "content": ["测试content" ], 经过对比title 和content字段,发现co ...

  2. [xUI] ligerUI开发框架简介和搭建

    ligerUI开发者:谢略,网名daomi API:         http://api.ligerui.com/ 演示地址:  http://demo.ligerui.com/ 源码下载:  ht ...

  3. Android消息推送完美方案

    转自:http://bbs.hiapk.com/thread-4652657-1-1.html 推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原 ...

  4. 重构20-Extract Subclass(提取父类)

    当一个类中的某些方法并不是面向所有的类时,可以使用该重构将其迁移到子类中.我这里举的例子十分简单,它包含一个Registration类,该类处理与学生注册课程相关的所有信息. public class ...

  5. java 解析XML文档

    Java 解析XML文档 一.解析XML文档方式: 1.DOM方式:将整个XML文档读取到内存中,按照XML文件的树状结构图进行解析. 2.SAX方式:基于事件的解析,只需要加载XML中的部分数据,优 ...

  6. apache2.4的安装与卸载

    安装sudo apt-get install apache2,这不是源码安装的方式,产生的apache地址在/etc/apache2,配置文件是apache2.conf如果浏览器输入127.0.0.1 ...

  7. VMware系统运维(八)vCenter Server安装

    1.终于开始安装vCenter Server了,需要配置数据源哦! 2.下一步 3.接受协议,下一步 4.输入许可密钥,也可以后面再输入,下一步 5.选择数据源,即我们前面配置的系统DSN,下一步 6 ...

  8. 隐藏自定义的tabbar之后,push到B视图,B视图的键盘工具条无法响应点击事件

    我的情况如下: 在TabbarViewController中隐藏了系统的tabbar,然后自定义tabbar,A B C D 4个视图都有UINavigationController,A视图 使用的是 ...

  9. poj 3980 取模运算

    取模运算 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10931   Accepted: 6618 Description ...

  10. PHP面向对象之将数据库的查询结果序列化成json格式

    <?php class link_mysql{ private $host,$uid,$pwd,$db,$link,$res; function link_mysql($_host,$_uid, ...