我不知道,如果是可以做到的,但我想解析一個非常簡單的JSON文件到字符串數組。使用正則表達式來分析一個非常簡單的JSON文件
示例文件:
["String1", "String2", "oneMoreString"]
到目前爲止,我想我會用掃描儀用一個模式來得到我的輸出,但沒能做到這一點。
ArrayList<String> strings = new ArrayList<String>();
File f = new File("src/sample.txt");
String pattern = "\\s*[\"\"]\\s*";
try {
InputStream is = new FileInputStream(f);
Scanner s = new Scanner(is);
s.useDelimiter(pattern);
while (s.hasNext()){
strings.add(s.next());
}
s.close();
is.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
因爲該模式顯然是錯誤的,因爲它認爲「」因爲它適合,但我想它不會被列入...:S
我也接受建議,可以任何其他方式工作,這可以解析。也許是一個JSON解析器?但由於文件太簡單了,我認爲沒有必要。
你可以看看GSON解析器http://code.google.com/p/google-gson/ – Sach
得到了我的路徑http://jackson.codehaus.org/,但似乎矯枉過正使用JSON解析器... – Daren
@達人,不,根本不會矯枉過正(使用傑克遜/吉森)。引入10行以上的正則表達式代碼來處理簡單的JSON字符串不是要走的路(IMO)。 –