在最簡單的事情上讓自己瘋狂。我有一個名爲config.txt的JSON文件。該文件如下所示。 { 「UsePipesInGuestData」:真 }在Android中將簡單的JSON文件加載到2維數組中
所有我想要做的是得到一個2維陣列,使得: 數組[0] = UsePipesInGuestData和 數組[1] =真
我已經努力了4小時各種嘗試,我最近如下: 私人無效getConfig(){// 功能來讀取的config.txt
FileInputStream is;
BufferedReader reader;
try {
final File configFile = new File(Environment.getExternalStorageDirectory().getPath() + "/guestlink/config.txt");
if (configFile.exists()) {
is = new FileInputStream(configFile);
reader = new BufferedReader(new InputStreamReader(is));
String line = reader.readLine();
while (line != null) {
line = reader.readLine();
if(line!= null) {
line = line.replace("\"", ""); //Strip out Quotes
line = line.replace(" ", ""); //Strip out Spaces
if ((!line.equals("{")) || (!line.equals("}"))) {
} else {
String[] configValue = line.split(":");
switch (configValue[0]) {
case "UsePipesInGuestData":
if (configValue[1].equals("true")) {
sharedPreferences.edit().putString("UsePipes", "true").apply();
} else {
sharedPreferences.edit().putString("UsePipes", "false").apply();
}
break;
}
}
}
}
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
我的配置信息似乎無法忽略其中{和}的行。
很明顯,有一個更簡單的方法。 JAVA似乎需要大量的代碼來做最簡單的事情。任何幫助是極大的讚賞。
你可以使用Gson https://stackoverflow.com/questions/29965764/how-to-parse-json-file-with-gson – tebitoq
可能的重複[如何解析Java中的JSON](https:// stackoverflow .com/questions/2591098/how-to-parse-json-in-java) –