java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.JSONArray

后端 未结 2 983
小鲜肉
小鲜肉 2020-12-18 06:05

I need to read a JsonArray data from a file (on an SD card) and store it into a SQLite database.

but I am getting a java.lang.ClassCastException:

2条回答
  •  鱼传尺愫
    2020-12-18 06:56

    You can first read the whole content of file into a String.

    FileInputStream fileInputStream = null;
    String data="";
    StringBuffer stringBuffer = new StringBuffer("");
    try{
        fileInputStream=new FileInputStream(filename);
        int i;
        while((i=fileInputStream.read())!=-1)
        {
            stringBuffer.append((char)i);
        }
        data = stringBuffer.toString();
        csvFile.delete();
    }
    catch(Exception e){
            LoggerUtil.printStackTrace(e);
    }
    finally{
        if(fileInputStream!=null){  
            fileInputStream.close();
        }
    }
    

    Now You will have the whole content into String ( data variable ).

    JSONParser parser = new JSONParser();
    org.json.simple.JSONArray jsonArray= (org.json.simple.JSONArray) parser.parse(data);
    

    After that you can use jsonArray as you want.

提交回复
热议问题