java.util.Vector 在 java 中解析 json 时无法转换为 java.util.ArrayList 错误

java.util.Vector cannot be cast to java.util.ArrayList error when json is parsed in java

我想使用 arraylist 而不是 vector,因为 ArrayList 提供更好的性能等。但是代码也可以使用 vector。我不明白当我使用json_encode时,有时它可以在arraylist中解析,有时在vector中解析。将 json 解析值转换为向量和数组列表有何不同?是来自 model/controller(此处使用 CI)还是我必须在 java json 解析器中做一些不同的事情?

codeIgniter 控制器:

$data = $this->member->view_members();
header('Content-Type: application/json');
echo json_encode($data);

CI 型号:

$queries = $this->db->get('members');
return $queries->result();

json:

[{  "id": "1",
    "name": "Magalamind Industries",
    "tableGroup": "Birgunj  rising round table 20",
    "email": "birgunj123@gmail.com",
    "position": "Senior Member",
    "address": "Birgunj",
    "phn": "9876543234",
    "date": "2016-05-03 15:53:51",
    "active": "1"},
 {  "id": "2",
    "name": "Ram kumar Sharma",
    "tableGroup": "Lalitpur round table 5",
    "email": "ram@gmail.com",
    "position": "Recent Member",
    "address": "Patandhoka, lalitpur",
    "phn": "9876567890",
    "date": "2016-05-03 15:53:51",
    "active": "1"}]

java json 解析错误消息:java.util.Vector 无法转换为 java.util.ArrayList

ConnectionRequest connectionRequest = new ConnectionRequest() {    
    @Override
    protected void readResponse(InputStream input) throws IOException {
        JSONParser jSONParser = new JSONParser();
        Map<String,Object> parsedData = jSONParser.parse(new InputStreamReader(input));
        response = (ArrayList) parsedData.get("root"); //error msg: java.util.Vector cannot be cast to java.util.ArrayList
    }   
    .........
}

jSONParser.parse(new InputStreamReader(input));

是旧的解析方法(注意它在 javadoc 中已弃用),它使用 Hashtables 和 Vectors。

您需要使用

jSONParser.parseJSON(new InputStreamReader(input));

其中使用了HashMap和ArrayList