2012-08-29 119 views
1

我正在使用url来解析文件。现在我想读解析中的sdcard文件,我可以怎么做?从URLJSON解析不使用URL来读取android中的json文件

private static String url = "http://10.0.2.2/device1.json"; 
    JSONObject json = jParser.getJSONFromUrl(url); 
      devices = json.getJSONArray(TAG_DEVICES); 

现在我想在这里 我的代码

读取文件读取文件中的SD卡

java.io.File device = new java.io.File("/mnt/sdcard/device1.json"); 
    FileReader device_Fr = new FileReader(device); 

下怎么通过这个文件解析做到这一点?

回答

1

使用此代码

从文件中读取数据到串&通,为的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); 
+1

这工作对我来说......谢谢 –

+0

肯定会从下次照顾照顾 –

0

一种方法是读取json并将其存储在一个字符串中。然后分析它是这样的:

String jsonStr = // read from file 
    JSONObject json = new JSONObject(jsonStr); 
    // parse 
  

  devices = json.getJSONArray(TAG_DEVICES);