2

我搜索了所有相关线程,但没有成功,欢迎任何相关答案。

.json我在文件夹内的 SDCard 中有一个 JSON 数据作为.NewFolder文件,我想读取文件中的数据并解析,找到时间戳,如果时间戳不同,我想重写它。

下面是我使用的代码,我得到了数据aBuffer和值,jdata_fromfile但是在解析时我得到了 nullPointerException。

myFile = new File(Environment.getExternalStorageDirectory() + "/.NewFolder/" +str.get(i));
if(myFile.exists())
{
    FileInputStream fIn = new FileInputStream(myFile);
    BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
    String aDataRow = "";
    String aBuffer = "";
    while ((aDataRow = myReader.readLine()) != null) 
    {
        aBuffer += aDataRow;
    }
    myReader.close();
    jdata_fromfile = new JSONObject(aBuffer);

    String timestamp_fromfile = jdata_fromfile.getString(str_timestamp.get(i));
    timestamp_val_fromfile.add(timestamp_fromfile);
}
else
{
    myFile.createNewFile();
    writedate_tofile(value_json.toString());
    timestamp_val_fromfile.add("xxx");
}

而且,当我在记事本中打开文件时,它都在一行中,仅显示部分数据,当我复制到http://jsonformatter.curiousconcept.com时

我得到了全部数据和一个有效的 json 。当我这样做时,我可以在记事本中看到其余数据 - ctrl+end 并按 enter 。

为什么会这样,我哪里出错了?

这是我的错误 - 日志

02-13 18:44:52.109: E/aBuffer value(7134):  is {"Status":{"Itemlist":[{"x - Drinks":{"Details":[{"type":"","image":"","price":"","name":"Minerals"},{"type":"","image":"","price":"","name":"Milk Shakes"},{"type":"","image":"","price":"","name":"Milk"},{"type":"","image":"","price":"","name":"Mineral Water"},{"type":"","image":"","price":"","name":"Hot Beverages"},{"type":"","image":"","price":"","name":"Chocolate Muffin and Ice Cream"}, ........................................................................{"type":"","image":"","price":"","name":"Blu
02-13 18:44:52.189: E/jdata_fromfile value(7134):  is {"x Time_stamp":"2013-02-12 12:30:00","Status":{"Itemlist":[{"x - Sides":{"Details":[{"type":"","image":"","name":"Onion Rings (6)","price":""},{"type":"","image":"","name":"Sausage Portion (Minimum 2 per portion)","price":""},.................................................................................................................... ,"x - Burgers":{"Details":[{"type":"","image":"","name":"x 5oz Burger","price":""},{"type":"","image":"","name":"x 5oz Cheese Burger","price":""},{"type":"","image":"","name":"x 5oz with Bacon and Cheese","price":""},{"type":"","imag
02-13 18:44:52.189: E/Reading from internal file - Exception e(7134): java.lang.NullPointerException

提前致谢 。

4

1 回答 1

1

我不知道上面的代码有什么问题,但这里是使用不同 JSON 库的解决方案。

我在我的一个 android 应用程序中使用了以下代码,它非常适合从 android 文件系统读取任何 json 文件。我没有使用 Android 的 JSON 库,而是使用json-simple.jarhttp://code.google.com/p/json-simple/

将上面的 jar 放在你的 Android 项目的 libs 文件夹中。并在.Manifest文件中设置以下权限。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

然后将以下代码放在单独的线程中。

JSONParser parser  = new JSONParser();
try { 
    String PATH = Environment.getExternalStorageDirectory() + "/Download/list.json";
    Object object = parser.parse(new FileReader(PATH));             
    JSONObject jsonObject = (JSONObject) object;
    //Use JSONArray instead of JSONObject if json file contains array of JSONs
    //
    // Do anything with jsonObject  
    //
} catch(Exception e) {
    // TODO: handle exception
}

我的list.json文件位于 location /mnt/sdcard/Download

这里是一个在简单的 java 项目中使用上述库的示例,供参考。

于 2013-03-11T17:17:15.727 回答