简体   繁体   English

java.lang.ClassCastException: 类 net.sf.json.JSONObject 不能转换为类 net.sf.json.JSONArray

[英]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.

相关问题 将net.sf.json.JSONObject与Google地理编码一起使用时获取java.lang.ClassCastException - Getting a java.lang.ClassCastException when using net.sf.json.JSONObject with Googles Geocoding java.lang.ClassNotFoundException: net.sf.json.JSONObject - java.lang.ClassNotFoundException: net.sf.json.JSONObject 循环调用Google Maps API以对数十个地址进行地理编码并获取“ java.lang.String无法转换为net.sf.json.JSONObject” - Call Google Maps API in a loop to geocode dozens of addresses and get “java.lang.String cannot be cast to net.sf.json.JSONObject” 转义为net.sf.json.JSONObject - Escape for net.sf.json.JSONObject java如何将字符串转换为net.sf.json.JSONObject - java how to convert a string to net.sf.json.JSONObject java.lang.ClassCastException:无法将java.lang.String强制转换为net.minidev.json.JSONObject - java.lang.ClassCastException: java.lang.String cannot be cast to net.minidev.json.JSONObject net.sf.json.JSONObject会在不期望的地方添加反斜杠吗? - net.sf.json.JSONObject adds backslashes where it's not expected? java.lang.NoClassDefFoundError:net / sf / json / JSONArray - java.lang.NoClassDefFoundError : net/sf/json/JSONArray java.lang.ClassCastException:net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory无法转换为org.hibernate.cache.RegionFactory - java.lang.ClassCastException: net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory cannot be cast to org.hibernate.cache.RegionFactory java.lang.ClassCastException:org.json.simple.JSONArray无法强制转换为org.json.JSONArray - java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM