Android编程读写json文件,Android如何从SD卡读取json文件(文本文件),并将数据显示到textview...

使用

File从SD卡读取JSON文件,并使用

JSONObject进行解析.

这里有一个起始码:

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

public class ReadJsonFile {

public ReadFile (){

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,fc.size());

jsonStr = Charset.defaultCharset().decode(bb).toString();

}

catch(Exception e){

e.printStackTrace();

}

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 {\n \"id\": \"3\",\n \"title\": \"Ahmad Shah\",\n {\n \"id\": \"4\",\n \"title\": \"Mohsin Shah\",\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 parsedData = new HashMap();

// 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();

}

}

}

不要忘记在你的主播上添加读取存储:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值