[英]JSON Parsing without using URL to read json file in android
我正在使用url提取文件进行解析。 现在我想读取sdcard中的文件进行解析。我该怎么做? 这是我的代码
从网址读取文件
private static String url = "http://10.0.2.2/device1.json";
JSONObject json = jParser.getJSONFromUrl(url);
devices = json.getJSONArray(TAG_DEVICES);
现在我想通过读取sdcard中的文件来执行此操作
java.io.File device = new java.io.File("/mnt/sdcard/device1.json");
FileReader device_Fr = new FileReader(device);
接下来如何传递此文件进行解析?
使用此代码
从文件中读取数据到字符串并将其传递给JSONObject。
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line, results = "";
while( ( line = reader.readLine() ) != null)
{
results += line;
}
reader.close();
JSONObject obj = new JSONObject(results);
一种方法是读取json并将其存储在字符串中。 然后像这样解析它:
String jsonStr = // read from file
JSONObject json = new JSONObject( jsonStr);
// parse
devices = json.getJSONArray(TAG_DEVICES);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.