如何读取Character的jsonArray并将字符存储在ArrayList中?

时间:2019-05-05 17:54:38

标签: java json arraylist

我在json文件中有这个json字符串

{"Q":[0,1,2,3],"q_0":0,"F":[3],"delta":[[0,1],[0,2],[3,2],[0,1]],"segma":[0,1]}

我想读取segma数组并将其存储为字符数组列表

 static ArrayList<Long> Q;
    static ArrayList<Character> segma;
    static ArrayList<Long> F;
    static Long q_0;
    static Long deadStat;
    static ArrayList<ArrayList<Integer>> delta;

parseDFAObject函数

 private static void parseDFAObject(JSONObject dfa) {
        //Get employee object within list
        Automat.Q = (ArrayList<Long>) dfa.get("Q");
        System.out.println(Q);
        //Get employee first name
        Automat.q_0 = (Long) dfa.get("q_0");
        System.out.println(q_0);

        Automat.F = (ArrayList<Long>) dfa.get("F");
        System.out.println(F);



        Automat.delta = (ArrayList) dfa.get("delta");
        System.out.println(delta);
        JSONArray ja;
        ja = (JSONArray)dfa.get("segma");
        Character[] ch = (Character[]) ja.toArray();
        for(int s=0;s<ch.length;s++){
            Automat.segma.add(s,ch[s]);}

        System.out.println(segma);
    }

checkAccepte函数

    public static boolean checkAccepte(String s) {
        int qs;
        qs = q_0.intValue();
        int qalpha;
        for (int i = 0; i <= s.length(); i++) {
            Character c = (char) s.charAt(i);
            qalpha = segma.indexOf(c);
            qs =Q.indexOf(delta.get(qs).get(qalpha));
        }
        return false;
    }

输入为0110 主要功能

  public static void main(String[] args) {

        Scanner input =new Scanner(System.in);
        //JSON parser object to parse read file
        JSONParser jsonParser = new JSONParser();

        try (FileReader reader = new FileReader("DFA.json")) {
            //Read JSON file
            Object obj = jsonParser.parse(reader);

            JSONObject DFA1 = (JSONObject) obj;
            System.out.println(DFA1);


            parseDFAObject(DFA1);

            if( checkAccepte(input.next()){
                  System.out.println("ACCEPTED ^_^");
              }
            else{
                  System.out.println("NOT ACCEPTED !!");
                }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

我有此异常

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Character;
    at automat.Automat.parseDFAObject(Automat.java:150)
    at automat.Automat.main(Automat.java:111)

我试图这样做

Automat.segma = (ArrayList<Character>) dfa.get("segma");

代替parseDFAObject函数中的这段代码

 JSONArray ja;
        ja = (JSONArray)dfa.get("segma");
        Character[] ch = (Character[]) ja.toArray();
            Automat.segma.add(s,ch[s]);

并且当我在checkAccepte函数中使用java segma.indexOf(c);时出现此异常,并且它返回-1

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(ArrayList.java:422)
    at java.util.ArrayList.get(ArrayList.java:435)
    at automat.Automat.checkAccepte(Automat.java:40)
    at automat.Automat.main(Automat.java:113)
``

1 个答案:

答案 0 :(得分:0)

这里:

"segma":[0,1]

ja = (JSONArray)dfa.get("segma");
Character[] ch = (Character[]) ja.toArray();

什么使您认为 segma 将是字符数组?就像现在所写的那样,它将是一个数字的数组。您的 Q 数组具有几乎相同的示例数据,您将其视为List<Long>。不要指望从JSON解析器出来的另一个数组具有不同的类型!

如果出于某种原因要将[0,1]视为字符数组,则必须将传入的Long列表转换到该表示形式中。 JSON解析器本身并不能为您做到这一点!