Android的内部存储,如何正确地分析JSON文本文件 [英] Android Internal Storage, how to properly parse a JSON text file

查看:113
本文介绍了Android的内部存储,如何正确地分析JSON文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Android应用程序创造与JSON对象的文本文件,并将其写入到内部存储器。我有以下的code做到这一点:

I'm creating an Android app which creates a text file with JSON objects and writes it to internal storage. I have the following code to do that:

JSONObject myJSON = new JSONObject();
//Set the JSON object with website, length and Id (time-stamp)
try {
    myJSON.put("Length", trim)
    .put("Website", data)
    .put("Id", tx);
} catch (JSONException e1) {
    e1.printStackTrace();
}

//Convert JSON object to a string and add a comma
String myJSONString = myJSON.toString();
myJSONString += ", ";

try {
     FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
     fos.write(myJSONString.getBytes());
     fos.close();
     //Log.d(TAG, "Written to file");

} catch (Exception e) {
    Log.d(TAG, "cought");
    e.printStackTrace();
}

现在我得到一个文本文件,它看起来像这样:

Now I get a text file which looks like so:

{"Id":"20101211T155146","Length":10}, {"Id":"20101211T155155","Length":10},
{"Id":"20101211T155203","Length":10}, {"Id":"20101211T155252","Length":10}, 

我想现在收集的数据,在JSON文件。该应用程序需要编写,存储和检索JSON。问题是,当我去使用解析从文件中的JSON对象:

I'd like to now collect that data in the JSON file. The app needs to write, store and retrieve the JSON. The problem is when I go to parse the JSON objects from the file using:

String x = "";
InputStream is = this.getResources().openRawResource(R.raw.pwh);
byte [] buffer = new byte[is.available()];
while (is.read(buffer) != -1);
String jsontext = new String(buffer);
JSONArray entries = new JSONArray(jsontext);

x = "JSON parsed.\nThere are [" + entries.length() + "]\n\n";

int i;
for (i=0;i<entries.length();i++)
{
    JSONObject post = entries.getJSONObject(i);
    x += "------------\n";
    x += "Id:" + post.getString("Id") + "\n";
    x += "Length:" + post.getString("Length") + "\n\n";
}

它抛出一个错误。我得到了解析code,从一个伟大的教程在:的 http://www.ibm.com/developerworks/web/library/x-andbene1/?ca=drs-#author1 在该示例中,code为期望括号整个文件和最后的对象之后没有逗号。所以,我需要:

It throws an error. I got the parsing code from a great tutorial at: http://www.ibm.com/developerworks/web/library/x-andbene1/?ca=drs-#author1 In that example, the code is expecting brackets around the whole file and no comma after the last object. So I would need:

[{"Id":"20101211T155146","Length":10}, {"Id":"20101211T155155","Length":10},
{"Id":"20101211T155203","Length":10}, {"Id":"20101211T155252","Length":10}]

不过,我写那些JSON线一下午时间在我的code;我该如何操作JSON文本文件,把它预期的格式?

But I write those JSON lines one at time in my code; How can I manipulate the JSON text file to get it in the expected format?

更新:

问题依旧,如果用户写JSON数组到该文件,然后回来,并再次改变它,你在该文件中获得的双JSON数组。像这样:

The problem is still that if the user writes the JSON array to the file and then comes back and changes it again, you get two JSON arrays in that file. Like so:

[
     {
          "phonenumber": "15555215554",
          "time": "20110113T173835",
          "username": "edit username",
          "email": " edit email",
          "password": "edit password"
     }
][
     {
          "phonenumber": "15555215554",
          "time": "20110113T173900",
          "username": "edit username",
          "email": " edit email",
          "password": "edit password"
     },
     {
          "phonenumber": "15555215554",
          "time": "20110113T173900",
          "username": "edit username",
          "email": " edit email",
          "password": "edit password"
     }
]

我怎样才能读取第一个阵列,添加第二个,然后重新写出来与两个数组文件合并成一个?

How can I read the first array, add the second and then re-write the file with both arrays merged into one?

推荐答案

您需要使用 JSONArray 来创建 JSON对象的数组。一旦你做一个toString(),您将有预期的括号内。你并不需要手动或者添加逗号。

You need to use JSONArray to create an array of JSON objects. Once you do a toString(), you will have the expected brackets. You do not need to manually add the commas either.

看看 http://developer.android.com/reference/组织/ JSON / JSONArray.html 更多信息

这篇关于Android的内部存储,如何正确地分析JSON文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆