机器人如何从SD卡和显示数据到TextView中读取JSON文件(文本文件) [英] Android How can I read json file(text file) from SD Card and display data into textview
本文介绍了机器人如何从SD卡和显示数据到TextView中读取JSON文件(文本文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的文本文件到我SdCard.now我想解析通过JSON解析器这个文件,我想分析和读取这个文件,并显示数据到我TextView的,我怎么能做到这一点?
I have the following textfile into my SdCard.now i want to parse this file via Json parser,and i want to parse and read this file and display data into my textview,how i can do this?
{
"data": [
{
"id": "1",
"title": "Farhan Shah",
"duration": 10,
},
{
"id": "2",
"title": "Noman Shah",
"duration": 10,
},
{
"id": "3",
"title": "Ahmad Shah",
"duration": 10,
},
{
"id": "4",
"title": "Mohsin Shah",
"duration": 10,
},
{
"id": "5",
"title": "Haris Shah",
"duration": 10,
}
]
}
这是在code:
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"textarabics.txt");
String UTF8 = "utf8";
int BUFFER_SIZE = 8192;
//Read text from file
StringBuilder text = new StringBuilder();
我得到的文件,但现在我已经不知道我怎么能分析数据?
i get the file,but now i have no idea that how i can parse the data?
推荐答案
使用文件中读取JSON文件从SD卡,并使用的JSONObject 解析它。
Read the JSON file using File from SD card and use JSONObject to parse it.
下面的出发code:
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
try {
File yourFile = new File(Environment.getExternalStorageDirectory(), "path/to/the/file/inside_the_sdcard/textarabics.txt");
FileInputStream stream = new FileInputStream(yourFile);
String jsonStr = null;
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
jsonStr = Charset.defaultCharset().decode(bb).toString();
}
finally {
stream.close();
}
/* String jsonStr = "{\n\"data\": [\n {\n \"id\": \"1\",\n \"title\": \"Farhan Shah\",\n \"duration\": 10\n },\n {\n \"id\": \"2\",\n \"title\": \"Noman Shah\",\n \"duration\": 10\n },\n {\n \"id\": \"3\",\n \"title\": \"Ahmad Shah\",\n \"duration\": 10\n },\n {\n \"id\": \"4\",\n \"title\": \"Mohsin Shah\",\n \"duration\": 10\n },\n {\n \"id\": \"5\",\n \"title\": \"Haris Shah\",\n \"duration\": 10\n }\n ]\n\n}\n";
*/
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting data JSON Array nodes
JSONArray data = jsonObj.getJSONArray("data");
// looping through All nodes
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String id = c.getString("id");
String title = c.getString("title");
String duration = c.getString("duration");
//use > int id = c.getInt("duration"); if you want get an int
// tmp hashmap for single node
HashMap<String, String> parsedData = new HashMap<String, String>();
// adding each child node to HashMap key => value
parsedData.put("id", id);
parsedData.put("title", title);
parsedData.put("duration", duration);
// do what do you want on your interface
}
} catch (Exception e) {
e.printStackTrace();
}
Ddon't忘记添加读取存储在您的mainfest:
Ddon't forget to add read storage on your mainfest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
这篇关于机器人如何从SD卡和显示数据到TextView中读取JSON文件(文本文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文