【问题标题】:how to read jsonArray of Character and store the characters in ArrayList?如何读取字符的jsonArray并将字符存储在ArrayList中?
【发布时间】:2019-09-23 11:24:12
【问题描述】:

我在 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)
``

【问题讨论】:

    标签: java json arraylist


    【解决方案1】:

    这里:

    "segma":[0,1]
    

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

    究竟是什么让你认为 segma 将是一个字符数组?正如现在所写的,它将是一个数字数组。您的 Q 数组具有几乎相同的示例数据,并且您将其视为List&lt;Long&gt;。不要指望从 JSON 解析器出来的另一个数组有不同的类型!

    如果出于某种原因您想将 [0,1] 视为字符数组,那么您必须将传入的 Long 列表 转换 为该表示形式。 JSON 解析器本身不会为您执行此操作!

    【讨论】:

    • 我必须从文件中读取多个对象,而 Sigma 数组可能包含字母、数字或两者兼有;那么你能告诉我如何转换 Long 的传入列表吗,或者你能建议另一种结构来存储 Sigma 数组吗?提前谢谢你
    • Json 不认识字符。它知道数字或字符串。故事结局。因此,如果您需要我们的帮助,您必须向我们展示不同的输入可能性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    相关资源
    最近更新 更多