Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.ClassCastException:无法将类net.sf.json.JSONObject强制转换为类net.sf.json.JSONArray_Java_Json - Fatal编程技术网

java.lang.ClassCastException:无法将类net.sf.json.JSONObject强制转换为类net.sf.json.JSONArray

java.lang.ClassCastException:无法将类net.sf.json.JSONObject强制转换为类net.sf.json.JSONArray,java,json,Java,Json,我对JSON有一个问题。 在我的代码中,在这一行引发了一个异常 String jsontxt = IOUtils.toString(new FileInputStream(Filename), "UTF-8"); JSONArray root = (JSONArray) JSONSerializer.toJSON(jsontxt); jsontxt是存储所有JSON的字符串 出于某种原因,我有一个例外: Exception in thread "main" java.lang.ClassCa

我对JSON有一个问题。 在我的代码中,在这一行引发了一个异常

String jsontxt = IOUtils.toString(new FileInputStream(Filename), "UTF-8");
JSONArray  root = (JSONArray) JSONSerializer.toJSON(jsontxt);
jsontxt是存储所有JSON的字符串

出于某种原因,我有一个例外:

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')
我使用Lint验证了我的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"
        }
    ]
}
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"
        }
    ]
}
编辑:我找到了解决问题的办法。多亏了Arnaud Claudel,我注意到我的JSON一开始只是一个对象,而不是数组。因此,这样做修复了我的错误:

JSONObject  root = (JSONObject) JSONSerializer.toJSON(jsontxt);

问题是您有一个JSON对象,而不是数组


因此,您不能将其强制转换为
JSONArray
,也不能将其强制转换为
JSONObject

您可以打印
jsontxt
并将其添加到您的问题中吗?图书馆说它是一个物体而不是一个物体array@ArnaudClaudeljsontxt是一个字符串。我编辑了上面的代码。是的,我找到了答案,谢谢:)。我对我的问题进行了编辑,只是不知道如何将问题标记为已解决