まあ問題はJSONがロードメソッドでファイルを読み込む間です。私はコメンテントで説明します。 注:多くのコードは、私はそれのうちの一つだけの値を取得することができるので、その後の問題は、loadメソッドに固執JSONはちょうど1つの値をロードします
private static final String ID = "moti_data";
public static void save(Context context) throws IOException, JSONException {
JSONArray arr = new JSONArray();
JSONObject obj = new JSONObject();
for (int i = 0; i < notify1.length; i++)
obj.put("n1_" + i, notify1[i]);
arr.put(obj);
//while testing ar.toString() was arr[{"n1_0":1,"n1_1":15,"n1_2":23,"n1_3":0}]
//it is clear that it contains 4 values but arr.length() will give 1
String file = arr.toString();
FileOutputStream output = context.openFileOutput(ID, MODE_PRIVATE);
output.write(file.getBytes());
output.close();
}
よりクリアな視界のために省略されています。また、他のすべての値は正しくありません - 0です。私は数分の間、それを言葉にすることができたとき、arr [{"n1_0":1、 "n1_1":0、 "n1_2":0、 "n1_3" ]。 S0ちょうど最初の値は正しい
public static void load(Context context) throws IOException, JSONException {
//reading data
FileInputStream input = context.openFileInput(ID);
BufferedInputStream bi = new BufferedInputStream(input);
StringBuffer buffer = new StringBuffer();
while (bi.available() != 0) {
char c = (char) bi.read();
buffer.append(c);
}
bi.close();
input.close();
//JSON
JSONArray data = new JSONArray(buffer.toString());
for (int i = 0; i < data.length(); i++)
notify1[i] = data.getJSONObject(i).getInt("n1_" + i);
}
私は何をやっても、私は1つのループとjsonの例外を取得しようとしました。しかし、私は初期のtought問題は、出力が良かったので、保存していません。または私は間違っていますか?
EDDIT:コードに4つの値の6つのブロックがあります(ここでは明確にするために1つしかありません)。また、値はユーザによって変更されるため、ハードコードはできません。
ありがとうございました。洞察力ありがとう – raco