[英]java.lang.ClassCastException: class net.sf.json.JSONObject cannot be cast to class net.sf.json.JSONArray
i have a problem with JSON.我有 JSON 问题。 an exception is thrown at this line in my code在我的代码中的这一行抛出异常
String jsontxt = IOUtils.toString(new FileInputStream(Filename), "UTF-8");
JSONArray root = (JSONArray) JSONSerializer.toJSON(jsontxt);
jsontxt is a String where all my JSON is stored. jsontxt 是一个字符串,其中存储了我所有的 JSON。
and for some reason i have this exception:出于某种原因,我有这个例外:
Exception in thread "main" java.lang.ClassCastException: class net.sf.json.JSONObject cannot be cast to class net.sf.json.JSONArray (net.sf.json.JSONObject and net.sf.json.JSONArray are in unnamed module of loader 'app')
i validated my JSON file using Lint.我使用 Lint 验证了我的 JSON 文件。 Any idea where it might come from?知道它可能来自哪里吗?
here is my JSON :这是我的 JSON:
{
"numero_de_permis": "A0001",
"cycle": "2018-2020",
"heures_transferees_du_cycle_precedent": 2,
"activites": [{
"description": "Cours sur la déontologie",
"categorie": "cours",
"heures": 14,
"date": "2019-03-20"
},
{
"description": "Séminaire sur l'architecture contemporaine",
"categorie": "séminaire",
"heures": 10,
"date": "2019-01-07"
},
{
"description": "Rédaction pour le magazine Architecture moderne",
"categorie": "rédaction professionnelle",
"heures": 6,
"date": "2019-10-22"
},
{
"description": "Participation à un groupe de discussion sur le partage des projets architecturaux de plus de 20 ans ",
"categorie": "groupe de discussion",
"heures": 6,
"date": "2019-04-01"
},
{
"description": "Visite d'établissements architecturaux",
"categorie": "voyage",
"heures": 2,
"date": "2019-02-02"
}
]
}
the value of jsontxt is the same as my jsonfile meaning it loaded correctly: jsontxt 的值与我的 jsonfile 相同,这意味着它已正确加载:
{
"numero_de_permis": "A0001",
"cycle": "2018-2020",
"heures_transferees_du_cycle_precedent": 2,
"activites": [{
"description": "Cours sur la déontologie",
"categorie": "cours",
"heures": 14,
"date": "2019-03-20"
},
{
"description": "Séminaire sur l'architecture contemporaine",
"categorie": "séminaire",
"heures": 10,
"date": "2019-01-07"
},
{
"description": "Rédaction pour le magazine Architecture moderne",
"categorie": "rédaction professionnelle",
"heures": 6,
"date": "2019-10-22"
},
{
"description": "Participation à un groupe de discussion sur le partage des projets architecturaux de plus de 20 ans ",
"categorie": "groupe de discussion",
"heures": 6,
"date": "2019-04-01"
},
{
"description": "Visite d'établissements architecturaux",
"categorie": "voyage",
"heures": 2,
"date": "2019-02-02"
}
]
}
EDIT: i found a solution to my problem.编辑:我找到了解决我的问题的方法。 thanks to Arnaud Claudel, i noticed that my JSON at the start was just an object and not an array.感谢 Arnaud Claudel,我注意到我的 JSON 一开始只是一个对象而不是数组。 so doing this fixed my error:所以这样做修复了我的错误:
JSONObject root = (JSONObject) JSONSerializer.toJSON(jsontxt);
The problem is that you have a JSON Object, not an array.问题是你有一个 JSON 对象,而不是一个数组。
Therefore you can't cast it to a JSONArray
, cast it to JSONObject
.因此,您不能将其转换为JSONArray
,将其转换为JSONObject
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.