Kotlin - com.alibaba.fastjson.JSONException: default constructor not found
2018-02-06 20:41
591 查看
Kotlin - com.alibaba.fastjson.JSONException: default constructor not found:
Recently I fell in love with Kotlin. All .java files have become .kt.Such as the title, there is an exception when parsing JSON using the Kotlin data class.
com.alibaba.fastjson.JSONException: default constructor not found. class [${your Entity Class Name}] at com.alibaba.fastjson.util.JavaBeanInfo.build(JavaBeanInfo.java:465) at com.alibaba.fastjson.util.JavaBeanInfo.build(JavaBeanInfo.java:211) at com.alibaba.fastjson.parser.ParserConfig.createJavaBeanDeserializer(ParserConfig.java:643) at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:560) at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:373) at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:686) at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:663) at com.alibaba.fastjson.parser.DefaultJSONParser.parseArray(DefaultJSONParser.java:658) at com.alibaba.fastjson.JSON.parseArray(JSON.java:535) ...
I have two ways to solve this problem:
-> ———————————————-Gorgeous Dividing Line———————————————-1. Use Java Entity Class.
2. Add a Dependency.
<dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-reflect</artifactId> <version>${kotlin.version}</version> </dependency>
-> ———————————————-Gorgeous Dividing Line———————————————-
That is all right.
Here is an example:
^ JSON:{ "msgtype": "text", "text": { "content": "it is me." }, "at": { "atMobiles": [ "156xxxx8827", "189xxxx8325" ], "isAtAll": false } }
^^ Java Entity Class
public class Tjava { private String msgtype; private Text text; private At at; private class Text { private String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } } private class At { private Boolean isAtAll; private List<String> atMobiles; public Boolean getAtAll() { return isAtAll; } public void setAtAll(Boolean atAll) { isAtAll = atAll; } public List<String> getAtMobiles() { return atMobiles; } public void setAtMobiles(List<String> atMobiles) { this.atMobiles = atMobiles; } } public String getMsgtype() { return msgtype; } public void setMsgtype(String msgtype) { this.msgtype = msgtype; } public Text getText() { return text; } public void setText(Text text) { this.text = text; } public At getAt() { return at; } public void setAt(At at) { this.at = at; } }
^^ Kotlin Data Class
data class Tkotlin(val msgtype: String?, val text: Text?, val at: At?) { data class Text(val content: String?) data class At(val isAtAll: Boolean?, val atMobiles: List<String>?) }
^^^ Parse JSON
// Java public static void main(String[] args) { // Compiled successfully Tjava tjava = JSONObject.parseObject(json, Tjava.class); // if no dependency, can not compiled Tkotlin tkotlin = JSONObject.parseObject(json, Tkotlin.class); } // Kotlin fun main(args: Array<String>) { // Compiled successfully val tjava = JSONObject.parseObject(json, Tjava::class.java) // if no dependency, can not compiled val tkotlin = JSONObject.parseObject(json, Tkotlin::class.java) }
相关文章推荐
- kotlin使用com.alibaba.fastjson.JSONException: default constructor not found
- com.alibaba.fastjson.JSONException: default constructor not found. class com.anerfa.axdblue_code.ho
- com.alibaba.fastjson.JSONException: default constructor not found. class ……
- java.lang.ClassNotFoundException: com.alibaba.fastjson.JSON
- Caused by: java.lang.ClassNotFoundException: com.alibaba.fastjson.JSONObject
- Android开发fastjson错误:JSONException: default constructor not found. class com.xx...
- Exception in thread "main" com.alibaba.fastjson.JSONException: autoType is not support. com.accord.f
- com.alibaba.fastjson.JSONException: not close json text, token : :
- fastjson混淆后default constructor not found.
- 解决 com.alibaba.fastjson.JSONException: autoType is not support.
- 解决com.alibaba.fastjson.JSONException: autoType is not support
- com.alibaba.fastjson.JSONException: not close json text, token : error
- kotlin fastjson:defalut constructor not found
- com.alibaba.fastjson.JSONException: not close json text, token : error
- 解决ERROR - unregister mbean error javax.management.InstanceNotFoundException: com.alibaba.druid:type=
- com.alibaba.fastjson.JSONException: create instanc
- fastjson解析json时报错 class not has default constructor
- com.alibaba.fastjson.JSOnException:exepct '[' ,but {
- java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException
- java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException