我正在使用以下代码从平板电脑的内部存储中读取存储的JSON文件:
private String ReadFromLocal() {
File path = context.getExternalFilesDir(null);
File file = new File(path, "abc.json");
String ret = "No Data";
try {
FileInputStream fin = new FileInputStream(file);
ret = convertStreamToString(fin);
//Make sure you close all streams.
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null) {
sb.replace(0, 2, line);
}
reader.close();
return sb.toString();
}
该文件已成功检测并读取,但它从下到上而不是从上到下读取数据。这是原始的json,从资产中正确读取:
{
"0" : {
"workDetailsList" : {
"0" : {
"workTitle" : "Work 1",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 1,
"parentListItemPosition" : 1,
"allotted" : false
},
"1" : {
"workTitle" : "Work 2",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 2,
"parentListItemPosition" : 1,
"allotted" : false
},
"2" : {
"workTitle" : "Work 3",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 3,
"parentListItemPosition" : 1,
"allotted" : false
},
"3" : {
"workTitle" : "Work 4",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 4,
"parentListItemPosition" : 1,
"allotted" : false
}
},
"category" : "NA",
"itemHeader" : "Customer Voice 1",
"totalWorks" : 4,
"CustomerVoiceSerial" : 1,
"customerVoiceSerial" : 1
},
"1" : {
"workDetailsList" : {
"0" : {
"workTitle" : "Work 1",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 1,
"parentListItemPosition" : 2,
"allotted" : false
},
"1" : {
"workTitle" : "Work 2",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 2,
"parentListItemPosition" : 2,
"allotted" : false
},
"2" : {
"workTitle" : "Work 3",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 3,
"parentListItemPosition" : 2,
"allotted" : false
},
"3" : {
"workTitle" : "Work 4",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 4,
"parentListItemPosition" : 2,
"allotted" : false
}
},
"category" : "NA",
"itemHeader" : "Customer Voice 2",
"totalWorks" : 4,
"CustomerVoiceSerial" : 2,
"customerVoiceSerial" : 2
},
"2" : {
"workDetailsList" : {
"0" : {
"workTitle" : "Work 1",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 1,
"parentListItemPosition" : 3,
"allotted" : false
},
"1" : {
"workTitle" : "Work 2",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 2,
"parentListItemPosition" : 3,
"allotted" : false
},
"2" : {
"workTitle" : "Work 3",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 3,
"parentListItemPosition" : 3,
"allotted" : false
},
"3" : {
"workTitle" : "Work 4",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 4,
"parentListItemPosition" : 3,
"allotted" : false
}
},
"category" : "NA",
"itemHeader" : "Customer Voice 3",
"totalWorks" : 4,
"CustomerVoiceSerial" : 3,
"customerVoiceSerial" : 3
},
"3" : {
"workDetailsList" : {
"0" : {
"workTitle" : "Work 1",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 1,
"parentListItemPosition" : 4,
"allotted" : false
},
"1" : {
"workTitle" : "Work 2",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 2,
"parentListItemPosition" : 4,
"allotted" : false
},
"2" : {
"workTitle" : "Work 3",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 3,
"parentListItemPosition" : 4,
"allotted" : false
},
"3" : {
"workTitle" : "Work 4",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 4,
"parentListItemPosition" : 4,
"allotted" : false
}
},
"category" : "NA",
"itemHeader" : "Customer Voice 4",
"totalWorks" : 4,
"CustomerVoiceSerial" : 4,
"customerVoiceSerial" : 4
},
"4" : {
"workDetailsList" : {
"0" : {
"workTitle" : "Work 1",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 1,
"parentListItemPosition" : 5,
"allotted" : false
},
"1" : {
"workTitle" : "Work 2",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 2,
"parentListItemPosition" : 5,
"allotted" : false
},
"2" : {
"workTitle" : "Work 3",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 3,
"parentListItemPosition" : 5,
"allotted" : false
},
"3" : {
"workTitle" : "Work 4",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 4,
"parentListItemPosition" : 5,
"allotted" : false
}
},
"category" : "NA",
"itemHeader" : "Customer Voice 5",
"totalWorks" : 4,
"CustomerVoiceSerial" : 5,
"customerVoiceSerial" : 5
},
"5" : {
"workDetailsList" : {
"0" : {
"workTitle" : "Work 1",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 1,
"parentListItemPosition" : 6,
"allotted" : false
},
"1" : {
"workTitle" : "Work 2",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 2,
"parentListItemPosition" : 6,
"allotted" : false
},
"2" : {
"workTitle" : "Work 3",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 3,
"parentListItemPosition" : 6,
"allotted" : false
},
"3" : {
"workTitle" : "Work 4",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 4,
"parentListItemPosition" : 6,
"allotted" : false
}
},
"category" : "NA",
"itemHeader" : "Customer Voice 6",
"totalWorks" : 4,
"CustomerVoiceSerial" : 6,
"customerVoiceSerial" : 6
},
"6" : {
"workDetailsList" : {
"0" : {
"workTitle" : "Work 1",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 1,
"parentListItemPosition" : 7,
"allotted" : false
},
"1" : {
"workTitle" : "Work 2",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 2,
"parentListItemPosition" : 7,
"allotted" : false
},
"2" : {
"workTitle" : "Work 3",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 3,
"parentListItemPosition" : 7,
"allotted" : false
},
"3" : {
"workTitle" : "Work 4",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 4,
"parentListItemPosition" : 7,
"allotted" : false
}
},
"category" : "NA",
"itemHeader" : "Customer Voice 7",
"totalWorks" : 4,
"CustomerVoiceSerial" : 7,
"customerVoiceSerial" : 7
},
"7" : {
"workDetailsList" : {
"0" : {
"workTitle" : "Work 1",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 1,
"parentListItemPosition" : 8,
"allotted" : false
},
"1" : {
"workTitle" : "Work 2",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 2,
"parentListItemPosition" : 8,
"allotted" : false
},
"2" : {
"workTitle" : "Work 3",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 3,
"parentListItemPosition" : 8,
"allotted" : false
},
"3" : {
"workTitle" : "Work 4",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 4,
"parentListItemPosition" : 8,
"allotted" : false
}
},
"category" : "NA",
"itemHeader" : "Customer Voice 8",
"totalWorks" : 4,
"CustomerVoiceSerial" : 8,
"customerVoiceSerial" : 8
},
"8" : {
"workDetailsList" : {
"0" : {
"workTitle" : "Work 1",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 1,
"parentListItemPosition" : 9,
"allotted" : false
},
"1" : {
"workTitle" : "Work 2",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 2,
"parentListItemPosition" : 9,
"allotted" : false
},
"2" : {
"workTitle" : "Work 3",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 3,
"parentListItemPosition" : 9,
"allotted" : false
},
"3" : {
"workTitle" : "Work 4",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 4,
"parentListItemPosition" : 9,
"allotted" : false
}
},
"category" : "NA",
"itemHeader" : "Customer Voice 9",
"totalWorks" : 4,
"CustomerVoiceSerial" : 9,
"customerVoiceSerial" : 9
},
"9" : {
"workDetailsList" : {
"0" : {
"workTitle" : "Work 1",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 1,
"parentListItemPosition" : 10,
"allotted" : false
},
"1" : {
"workTitle" : "Work 2",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 2,
"parentListItemPosition" : 10,
"allotted" : false
},
"2" : {
"workTitle" : "Work 3",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 3,
"parentListItemPosition" : 10,
"allotted" : false
},
"3" : {
"workTitle" : "Work 4",
"workStatus" : "NOT ALLOTTED",
"timeRequired" : "0/60",
"selected" : false,
"workSerial" : 4,
"parentListItemPosition" : 10,
"allotted" : false
}
},
"category" : "NA",
"itemHeader" : "Customer Voice 10",
"totalWorks" : 4,
"CustomerVoiceSerial" : 10,
"customerVoiceSerial" : 10
}
}
但是使用描述的代码,这是我得到的字符串:
}}“customerVoiceSerial”:10“CustomerVoiceSerial”:10,“totalWorks”:4,"itemHeader" : "Customer Voice 10", "category" : "NA", }, } "allotted" : false "parentListItemPosition" : 10, "workSerial" : 4, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 4", "3" : { }, "allotted" : false "parentListItemPosition" : 10, "workSerial" : 3, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 3", "2" : { }, "allotted" : false "parentListItemPosition" : 10, "workSerial" : 2, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 2", "1" : { }, "allotted" : false "parentListItemPosition" : 10, "workSerial" : 1, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 1", "0" : { "workDetailsList" : {"9" : {}, "customerVoiceSerial" : 9 "CustomerVoiceSerial" : 9, "totalWorks" : 4, "itemHeader" : "Customer Voice 9", "category" : "NA", }, } "allotted" : false "parentListItemPosition" : 9, "workSerial" : 4, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 4", "3" : { }, "allotted" : false "parentListItemPosition" : 9, "workSerial" : 3, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 3", "2" : { }, "allotted" : false "parentListItemPosition" : 9, "workSerial" : 2, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 2", "1" : { }, "allotted" : false "parentListItemPosition" : 9, "workSerial" : 1, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 1", "0" : { "workDetailsList" : {"8" : {}, "customerVoiceSerial" : 8 "CustomerVoiceSerial" : 8, "totalWorks" : 4, "itemHeader" : "Customer Voice 8", "category" : "NA", }, } "allotted" : false "parentListItemPosition" : 8, "workSerial" : 4, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 4", "3" : { }, "allotted" : false "parentListItemPosition" : 8, "workSerial" : 3, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 3", "2" : { }, "allotted" : false "parentListItemPosition" : 8, "workSerial" : 2, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 2", "1" : { }, "allotted" : false "parentListItemPosition" : 8, "workSerial" : 1, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 1", "0" : { "workDetailsList" : {"7" : {}, "customerVoiceSerial" : 7 "CustomerVoiceSerial" : 7, "totalWorks" : 4, "itemHeader" : "Customer Voice 7", "category" : "NA", }, } "allotted" : false "parentListItemPosition" : 7, "workSerial" : 4, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 4", "3" : { }, "allotted" : false "parentListItemPosition" : 7, "workSerial" : 3, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 3", "2" : { }, "allotted" : false "parentListItemPosition" : 7, "workSerial" : 2, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 2", "1" : { }, "allotted" : false "parentListItemPosition" : 7, "workSerial" : 1, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 1", "0" : { "workDetailsList" : {"6" : {}, "customerVoiceSerial" : 6 "CustomerVoiceSerial" : 6, "totalWorks" : 4, "itemHeader" : "Customer Voice 6", "category" : "NA", }, } "allotted" : false "parentListItemPosition" : 6, "workSerial" : 4, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 4", "3" : { }, "allotted" : false "parentListItemPosition" : 6, "workSerial" : 3, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 3", "2" : { }, "allotted" : false "parentListItemPosition" : 6, "workSerial" : 2, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 2", "1" : { }, "allotted" : false "parentListItemPosition" : 6, "workSerial" : 1, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 1", "0" : { "workDetailsList" : {"5" : {}, "customerVoiceSerial" : 5 "CustomerVoiceSerial" : 5, "totalWorks" : 4, "itemHeader" : "Customer Voice 5", "category" : "NA", }, } "allotted" : false "parentListItemPosition" : 5, "workSerial" : 4, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 4", "3" : { }, "allotted" : false "parentListItemPosition" : 5, "workSerial" : 3, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 3", "2" : { }, "allotted" : false "parentListItemPosition" : 5, "workSerial" : 2, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 2", "1" : { }, "allotted" : false "parentListItemPosition" : 5, "workSerial" : 1, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 1", "0" : { "workDetailsList" : {"4" : {}, "customerVoiceSerial" : 4 "CustomerVoiceSerial" : 4, "totalWorks" : 4, "itemHeader" : "Customer Voice 4", "category" : "NA", }, } "allotted" : false "parentListItemPosition" : 4, "workSerial" : 4, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 4", "3" : { }, "allotted" : false "parentListItemPosition" : 4, "workSerial" : 3, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 3", "2" : { }, "allotted" : false "parentListItemPosition" : 4, "workSerial" : 2, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 2", "1" : { }, "allotted" : false "parentListItemPosition" : 4, "workSerial" : 1, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 1", "0" : { "workDetailsList" : {"3" : {}, "customerVoiceSerial" : 3 "CustomerVoiceSerial" : 3, "totalWorks" : 4, "itemHeader" : "Customer Voice 3", "category" : "NA", }, } "allotted" : false "parentListItemPosition" : 3, "workSerial" : 4, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 4", "3" : { }, "allotted" : false "parentListItemPosition" : 3, "workSerial" : 3, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 3", "2" : { }, "allotted" : false "parentListItemPosition" : 3, "workSerial" : 2, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 2", "1" : { }, "allotted" : false "parentListItemPosition" : 3, "workSerial" : 1, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 1", "0" : { "workDetailsList" : {"2" : {}, "customerVoiceSerial" : 2 "CustomerVoiceSerial" : 2, "totalWorks" : 4, "itemHeader" : "Customer Voice 2", "category" : "NA", }, } "allotted" : false "parentListItemPosition" : 2, "workSerial" : 4, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 4", "3" : { }, "allotted" : false "parentListItemPosition" : 2, "workSerial" : 3, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 3", "2" : { }, "allotted" : false "parentListItemPosition" : 2, "workSerial" : 2, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 2", "1" : { }, "allotted" : false "parentListItemPosition" : 2, "workSerial" : 1, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 1", "0" : { "workDetailsList" : {"1" : {}, "customerVoiceSerial" : 1 "CustomerVoiceSerial" : 1, "totalWorks" : 4, "itemHeader" : "Customer Voice 1", "category" : "NA", }, } "allotted" : false "parentListItemPosition" : 1, "workSerial" : 4, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 4", "3" : { }, "allotted" : false "parentListItemPosition" : 1, "workSerial" : 3, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 3", "2" : { }, "allotted" : false "parentListItemPosition" : 1, "workSerial" : 2, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 2", "1" : { }, "allotted" : false "parentListItemPosition" : 1, "workSerial" : 1, "selected" : false, "timeRequired" : "0/60", "workStatus" : "NOT ALLOTTED", "workTitle" : "Work 1", "0" : { "workDetailsList" : {"0" : {
我过去成功使用过上述方法。我试过搜索但找不到任何这样的csae。改变了平板电脑,但没有帮助。
BTW,Android / data / data / ...在我的情况下不是正确的路径,它是:
Android / data / com.example .......因此提到的方法正在使用中。