[英]JSON Parsing without using URL to read json file in android
I am using url to take the file for parsing. 我正在使用url提取文件进行解析。 Now i want read the file which is in sdcard for parsing.How can i do this?
现在我想读取sdcard中的文件进行解析。我该怎么做? here my code
这是我的代码
reading file from url 从网址读取文件
private static String url = "http://10.0.2.2/device1.json";
JSONObject json = jParser.getJSONFromUrl(url);
devices = json.getJSONArray(TAG_DEVICES);
now i want do this from reading the file in sdcard 现在我想通过读取sdcard中的文件来执行此操作
java.io.File device = new java.io.File("/mnt/sdcard/device1.json");
FileReader device_Fr = new FileReader(device);
next how to pass this file for parsing? 接下来如何传递此文件进行解析?
Use this code 使用此代码
read data from file into string & pass that to JSONObject. 从文件中读取数据到字符串并将其传递给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);
One way to do it is to read the json and store it in a string. 一种方法是读取json并将其存储在字符串中。 Then parse it like this:
然后像这样解析它:
String jsonStr = // read from file
JSONObject json = new JSONObject( jsonStr);
// parse
devices = json.getJSONArray(TAG_DEVICES);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.