Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 类强制转换异常[JSONArray不能强制转换为org.json.simple.JSONObject]_Java_Json_Gson - Fatal编程技术网

Java 类强制转换异常[JSONArray不能强制转换为org.json.simple.JSONObject]

Java 类强制转换异常[JSONArray不能强制转换为org.json.simple.JSONObject],java,json,gson,Java,Json,Gson,我有一个类似这样的json文件 [ { "id":"serve-coffee", "tags":[ { "name": "@tag1", "line": 1 } ], "description":"Coffee should not be served\n", "name":"Serve coffee", "keyword":"Feature", "line":2, "elements":[ {

我有一个类似这样的json文件

[ {
    "id":"serve-coffee",
    "tags":[ {
        "name": "@tag1", "line": 1
    }
    ],
    "description":"Coffee should not be served\n",
    "name":"Serve coffee",
    "keyword":"Feature",
    "line":2,
    "elements":[ {
        "id": "serve-coffee;buy-last-coffee", "tags":[ {
            "name": "@tag2", "line": 6
        }
        ],
        "description":"",
        "name":"Buy last coffee",
        "keyword":"Scenario",
        "line":7,
        "steps":[ {
            "name": "there are 1 coffees left in the machine", "keyword": "Given ", "line": 8
        }
        ,
        {
            "name": "I have deposited 1$", "keyword": "And ", "line": 9
        }
        ],
        "type":"scenario"
    }
    ],
    "uri":"src\/test\/resources\/traffic-remove-locations.feature"
}

]
我试图将上面的json文件转换为
JSONObject
。但是我得到了类强制转换异常
“java.lang.ClassCastException:org.json.simple.JSONArray无法强制转换为org.json.simple.JSONObject”

代码

但是当我更改行时
jsonObject=(jsonObject)对象
JSONArray JSONArray=(JSONObject)对象
异常消失。 但是如果我转换到
JSONArray
,那么如何从
JSONArray
获取
id、tags
description
等值呢。
请提供一个建议,尝试强制转换到JsonArray,然后借助JSON数组中的索引逐个强制转换访问对象

 Object object = parser.parse(new FileReader(fileName));         
 JsonArray  jsonArr = (JsonArray) object;   // Getting c
 jsonObject jsonObj = jsonArr.get(0);  //use index to access like a list

尝试强制转换到JsonArray,然后借助JSON数组中的索引逐个强制转换访问对象

 Object object = parser.parse(new FileReader(fileName));         
 JsonArray  jsonArr = (JsonArray) object;   // Getting c
 jsonObject jsonObj = jsonArr.get(0);  //use index to access like a list

JSON文件表示一个包含一个对象的数组。因此,如果这是一个Java数据结构,那么实际上就是这样做的:

int[] arr = {5};
int i = (int)arr;
这显然不起作用,因为不能将数组强制转换为单个对象。你真正想要做的是从数组中取出第一个元素。要继续Java示例,您需要执行以下操作

int[] arr = {5};
int i = (int)arr[0];
对于JSON内容,您的
parser.parse()
调用将返回JSONArray,而不是JSONObject。因此,您需要执行以下操作:

public static JSONObject convertFileToJSON(String fileName) throws ParseException {
    JSONParser parser = new JSONParser();
    JSONObject jsonObject = null;
    try {
        JSONArray array = (JSONArray) parser.parse(new FileReader(fileName));         
        jsonObject = array.getJsonObject(0);

    } catch (FileNotFoundException e) {

    } catch (IOException ioe) {

    }       
    return jsonObject;
}

JSON文件表示一个包含一个对象的数组。因此,如果这是一个Java数据结构,那么实际上就是这样做的:

int[] arr = {5};
int i = (int)arr;
这显然不起作用,因为不能将数组强制转换为单个对象。你真正想要做的是从数组中取出第一个元素。要继续Java示例,您需要执行以下操作

int[] arr = {5};
int i = (int)arr[0];
对于JSON内容,您的
parser.parse()
调用将返回JSONArray,而不是JSONObject。因此,您需要执行以下操作:

public static JSONObject convertFileToJSON(String fileName) throws ParseException {
    JSONParser parser = new JSONParser();
    JSONObject jsonObject = null;
    try {
        JSONArray array = (JSONArray) parser.parse(new FileReader(fileName));         
        jsonObject = array.getJsonObject(0);

    } catch (FileNotFoundException e) {

    } catch (IOException ioe) {

    }       
    return jsonObject;
}

我也面临同样的问题。我做了以下更改使其正常工作。 下面是我的JSON示例。 {

}

解析的解决方案: JSONObject obj=(JSONObject)jsonParser.parse(新文件读取器(FilepathDummy))


希望这能解决你的问题。

我也面临同样的问题。我做了以下更改使其正常工作。 下面是我的JSON示例。 {

}

解析的解决方案: JSONObject obj=(JSONObject)jsonParser.parse(新文件读取器(FilepathDummy))

希望这能解决你的问题