java: how to convert this string to ArrayList?

后端 未结 4 1596
说谎
说谎 2021-01-28 01:47
String text = \'[[\"item1\",\"item2\",\"item3\"], [\"some\", \"item\"], [\"far\", \"out\", \"string\"]]\';

I would like to iterate over each individual

4条回答
  •  抹茶落季
    2021-01-28 02:36

    You need to build a parser by hand. It's not hard, but it will take up time. In the previous comment you said you want an ArrayList of ArrayList... hmmm... good

    Just parse the string char by char and recognize each token by first defining recursive parsing rules. Recursive descendant parser rules are usually graphical, but I can try to use ABNF for you

    LIST = NIL / LIST_ITEM *( ',' SP LIST_ITEM)
    LIST_ITEM = NIL / '[' STRING_ITEM *(, SP STRING ITEM) ']'
    STRING_ITEM = '"' ANYCHAR '"'
    SP = space
    ANYCHAR = you know, anything that is not double quotes
    NIL = ''
    

    Another approach is to use Regular Expressions. Here are a couple of samples. First capture outer elements by

    (\[[^\]]*\])
    

    The above regex capture everything from '[' to the first ']', but you need to modify it or cut the brackets from your string (just drop first and last char)

    Then capture inner elements by

    (\"[^\"]\")
    

    Simple as the above

提交回复
热议问题