1.json工具类

package com.hyzn.fw.util;

import java.util.List;
import java.util.Map; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature; /**
* @ClassName: JsonUtil
* @Description: TODO
* @author xbq
* @version 1.0
* @date 2017-2-13 上午11:45:25
*/
public class JsonUtil { /**
* @Title: jsonToList
* @Description: TODO json 转为 List
* @param json
* @param obj
* @return
* @return: List
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List jsonToList(String json, Class clz){
return JSON.parseArray(json, clz);
} /**
* @Title: jsonToMap
* @Description: TODO json转Map
* @param json
* @param clz
* @return
* @return: Map
*/
@SuppressWarnings("rawtypes")
public static Map jsonToMap(String json) {
return JSON.parseObject(json,new TypeReference<Map<String,List>>(){});
} /**
* @Title: jsonToObject
* @Description: TODO json转实体
* @param json
* @param obj
* @return
* @return: Object
*/
public static Object jsonToObject(String json,Object obj){
return JSON.parseObject(json, obj.getClass());
} /**
* @Title: dealResponseJson
* @Description: TODO 实体 转为 Json
* @param obj
* @return
* @throws Exception
* @return: String
*
* QuoteFieldNames———-输出key时是否使用双引号,默认为true
* WriteMapNullValue——–是否输出值为null的字段,默认为false
* WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null
* WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null
* WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null
* WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null
*/
public static String ObjectToJson(Object obj) throws Exception {
return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteDateUseDateFormat,SerializerFeature.WriteNullNumberAsZero);
}
}

2.实体类

package com.hyzn.fw.controller;
/**
* @ClassName: Student
* @Description: TODO
* @author xbq
* @version 1.0
* @date 2017-3-2 上午11:42:20
*/
public class Student { private int ID;
private String NAME;
private String ADDRESS; public Student(int iD, String nAME, String aDDRESS) {
super();
ID = iD;
NAME = nAME;
ADDRESS = aDDRESS;
} public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getNAME() {
return NAME;
}
public void setNAME(String nAME) {
NAME = nAME;
}
public String getADDRESS() {
return ADDRESS;
}
public void setADDRESS(String aDDRESS) {
ADDRESS = aDDRESS;
} @Override
public String toString() {
return "Student [ID=" + ID + ", NAME=" + NAME + ", ADDRESS=" + ADDRESS
+ "]";
}
}

3.测试类

package com.hyzn.fw.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry; import com.alibaba.fastjson.JSONObject;
import com.hyzn.fw.util.JsonUtil; /**
* @ClassName: Test
* @Description: TODO
* @author xbq
* @version 1.0
* @date 2017-3-2 上午11:08:14
*/
public class Test { public static void main(String[] args) { List<Student> list = new ArrayList<Student>();
Student s = new Student(1,"张三","广州");
list.add(s);
s = new Student(2,"李四","广州");
list.add(s);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("TB_ROOM", JsonUtil.ObjectToJson(list));
jsonObject.put("TB_FLOOR", JsonUtil.ObjectToJson(list));
} catch (Exception e1) {
e1.printStackTrace();
}
String json = jsonObject.toJSONString();
System.out.println(json); JSONObject jsonobj = JSONObject.parseObject(json);
for(Entry<String, Object> jsonData : jsonobj.entrySet()){
System.out.println("jsonData.getValue() ==" + jsonData.getValue());
System.out.println("jsonData.getKey() ==" + jsonData.getKey());
List<Student> lists = JsonUtil.jsonToList(jsonData.getValue().toString(), Student.class);
System.out.println(lists);
}
}
}

运行测试类的时候 ,出现 异常,如下:

Exception in thread "main" com.alibaba.fastjson.JSONException: default constructor not found. class com.hyzn.fw.controller.Student
at com.alibaba.fastjson.util.DeserializeBeanInfo.computeSetters(DeserializeBeanInfo.java:150)
at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.<init>(JavaBeanDeserializer.java:43)
at com.alibaba.fastjson.parser.ParserConfig.createJavaBeanDeserializer(ParserConfig.java:332)
at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:323)
at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:256)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:542)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:521)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:516)
at com.alibaba.fastjson.JSON.parseArray(JSON.java:336)
at com.hyzn.fw.util.JsonUtil.jsonToList(JsonUtil.java:29)
at com.hyzn.fw.controller.Test.main(Test.java:39)

异常出现原因是 因为 实体类中没有空的构造方法,加上 空的构造方法,问题解决

com.alibaba.fastjson.JSONException: default constructor not found. class ……的更多相关文章

  1. JSON parse error: default constructor not found. class java.time.YearMonth; nested exception is com.alibaba.fastjson.JSONException: default constructor not found. class java.time.YearMonth

    java8新出的YearMonth可以方便的用来表示某个月.我的项目中使用springmvc来接收YearMonth类型的数据时发现 x-www-from-urlencoded 格式的数据可以使用&q ...

  2. redis通过json方案存取对象com.alibaba.fastjson.JSONException: syntax error, expect

    问题描述: redis基于json方案存取对象时报错:com.alibaba.fastjson.JSONException: syntax error, expect com.alibaba.fast ...

  3. 做文件上传下载时报这个错com.alibaba.fastjson.JSONException: illegal identifier : \

    ::-exec-] DEBUG c.i.e.m.I.insertDataEmebeding - <== Updates: ::-exec-] ERROR c.i.e.c.CaseArchiveC ...

  4. com.alibaba.fastjson.JSONException: For input string: "8200-12-31"

    https://www.cnblogs.com/mengjinluohua/p/5544987.html https://samebug.io/exceptions/458113/com.alibab ...

  5. com.alibaba.fastjson.JSONException: exepct '[', but error, pos 1, json : %255B%257B%2522list%2522%253A%255B%257B%2522itemId%2522%253A1369331%252C%2522num%2522%253A2%257D%255D%257D%255

    com.alibaba.fastjson.JSONException: exepct '[', but error, pos 1, json : %255B%257B%2522list%2522%25 ...

  6. fastjson转换包含date类型属性的对象时报错com.alibaba.fastjson.JSONException: For input string: "13:02:19"

    问题:time类型数据插入不进mysql数据库:调试的时候报如下错误: Caused by: java.lang.NumberFormatException: For input string: &q ...

  7. Caused by: com.alibaba.fastjson.JSONException: syntax error, expect {, actual [, pos 0, fastjson-version 1.2

    环境: vue.js 问题: 当添加评论时 重新查询数据刷新数据控制台异常Caused by: com.alibaba.fastjson.JSONException: syntax error, ex ...

  8. com.aliyun.openservices.shade.com.alibaba.fastjson.JSONException: exepct '[', but {, pos 1, line 1, column 2

    报错原因:你放的是一个非List的对象 却想取一个List对象出来 package test; import java.text.SimpleDateFormat; import java.util. ...

  9. 解决使用Redis时配置 fastjson反序列化报错 com.alibaba.fastjson.JSONException: autoType is not support

    1.问题描述 在使用redis时,配置自定义序列化redisTemplate为FastJsonRedisSerializer .  1 /** 2 * 自定义redis序列化器 3 */ 4 @Sup ...

随机推荐

  1. js队列的实现问题

    所谓队列就是排队的序列问题,有出有进,比如在银行排队办理业务,一般都是前一个办理完成后下一个自动进入队列 <script>  /* * 模拟队列 */ var Qu ={}; //构造函数 ...

  2. Java EE各种documentation

    总之通用的法则,google:"XxxName documentation" 例如,如果要查看JSTL的reference manual,google:jstl doc即可,ora ...

  3. LeetCode: Symmetric Tree 解题报告

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  4. js 记录

    (function(win,doc) { })(window,document)

  5. Tomcat (1) —— Mac下配置Tomcat Https/SSL

    Tomcat (1) -- Mac下配置Tomcat Https/SSL tomcat版本: tomcat-8.0.29 jdk版本: jdk1.8.0_65 参考来源: SSL/TLS Config ...

  6. phalcon的CLI应用

    CLI应用是命令行下执行的程序, 可以应用于定时任务,守护进程, 脚本, 公用命令等等. 最小的目录结构:app/config/config.phpapp/tasks/MainTask.phpapp/ ...

  7. 刘昕明:送给和我一样曾经浮躁过的PHP程序员

    作者注:2012年偶决定开始写博客了,不为别的,就希望可以通过博客记录我的成长历程同时也希望可以帮助一些刚毕业,刚入行业的兄弟姐们们.我们是一群充满浮躁.抱怨.迷茫的程序猿,想一想3年就这么过去了,社 ...

  8. [从jQuery看JavaScript]-注释(comments)

    jQuery片段: /*! * jQuery JavaScript Library v1.3.2 * http://jquery.com/ * * Copyright (c) 2009 John Re ...

  9. python with妙用

    class aa(): def bb(self): print("hhhh") return "hello world" def __enter__(self) ...

  10. JBPM——工作流概念

    一.概念          工作流(Workflow),就是"业务过程的部分或总体在计算机应用环境下的自己主动化",它主要解决的是"使在多个參与者之间依照某种提前定义的规 ...