gpt4 book ai didi

java - JSON 数据未加载回 ArrayList

转载 作者:行者123 更新时间:2023-12-01 12:37:53 26 4
gpt4 key购买 nike

我已成功将用户在应用程序中输入的数据保存到应用程序数据文件夹中的 JSON 文件中。

现在,我尝试在应用程序被销毁后将数据(如果存在)加载回应用程序。

这是我用来将其保存为 JSON 的代码

这是我的 JSON 保存和加载类:

public class CriminalIntentJSONSerializer {

private Context mContext;
private String mFilename;

public CriminalIntentJSONSerializer(Context c, String f) {
this.mContext = c;
this.mFilename = f;
}

public void saveCrimes(ArrayList<Crime> crimes) throws JSONException, IOException {
// Build an array in JSON
JSONArray array = new JSONArray();
for (Crime c: crimes) {
array.put(c.toJSON());
}
//Write the file to disk
Writer writer = null;
try {
OutputStream out = mContext.openFileOutput(mFilename, Context.MODE_PRIVATE);
writer = new OutputStreamWriter(out);
writer.write(array.toString());
} finally {
if (writer != null) {
writer.close();
}
}
}

public ArrayList<Crime> loadCrimes() throws IOException, JSONException {
ArrayList<Crime> crimes = new ArrayList<Crime>();
BufferedReader reader = null;
try {
// open and read the file into a StringBuilder
InputStream in = mContext.openFileInput(mFilename);
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder jsonString = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
// line breaks are omitted and irrelevant
jsonString.append(line);
}
// parse the JSON using JSONTokener
JSONArray array = (JSONArray) new JSONTokener(jsonString.toString()).nextValue();
// build the array of crimes from JSONObjects
for (int i = 0; i < array.length(); i++) {
crimes.add(new Crime(array.getJSONObject(i)));
}
} catch (FileNotFoundException e) {
// we will ignore this one, since it happens when we start fresh
} finally {
if (reader != null)
reader.close();
}
return crimes;
}
}

loadCrimes 似乎有问题。我尝试调试它,似乎它成功找到了 json 文件,我设法在调试器窗口中看到 json 内容:

enter image description here enter image description here enter image description here

它还将数据保存到 JSONArray 数组中,但当将其添加到 crimes 数组时,它不起作用。数组的大小保持为 0,因此实际上没有加载数据。它也不会抛出任何错误。

这是我的犯罪类(class):

public class Crime {

private static final String JSON_ID = "id";
private static final String JSON_TITLE = "title";
private static final String JSON_SOLVED = "solved";
private static final String JSON_DATE = "date";


private UUID mID;
private String mTitle;
private Date mDate;
private boolean mSolved;

public Crime() {
// Generate unique identifier
this.mID = UUID.randomUUID();
this.mDate = new Date();
}

public Crime(JSONObject json) throws JSONException {
mID = UUID.fromString(json.getString(JSON_ID));
if (json.has(JSON_TITLE)) {
mTitle = json.getString(JSON_TITLE);
}
mSolved = json.getBoolean(JSON_SOLVED);
mDate = new Date(json.getLong(JSON_DATE));
}

public String getmTitle() {
return mTitle;
}

public void setmTitle(String mTitle) {
this.mTitle = mTitle;
}

public UUID getmID() {
return mID;
}

public Date getmDate() {
return mDate;
}

public void setmDate(Date mDate) {
this.mDate = mDate;
}

public boolean ismSolved() {
return mSolved;
}

public void setmSolved(boolean mSolved) {
this.mSolved = mSolved;
}

@Override
public String toString() {
return mTitle;
}

public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
json.put(JSON_ID, getmID().toString());
json.put(JSON_TITLE, getmTitle());
json.put(JSON_SOLVED, ismSolved());
json.put(JSON_DATE, getmDate());

return json;
}
}

我正在使用 Genymotion 模拟器在 ICS 4.4.2 API 19 上运行该应用程序。在 Android Studio 0.8.6 上编码。

任何人都可以指出我到底做错了什么吗?

编辑。重新启动 Android Studio 后,它现在显示错误。这是堆栈跟踪。我发现这是一个简单的数据类型问题。解决了!:

08-20 15:34:13.019    2835-2835/? E/CrimeLab﹕ Error loading crimes:
org.json.JSONException: Value Wed Aug 20 15:26:15 GMT+00:00 2014 at date of type java.lang.String cannot be converted to long
at org.json.JSON.typeMismatch(JSON.java:100)
at org.json.JSONObject.getLong(JSONObject.java:482)
at com.apps.criminalintent.Crime.<init>(Crime.java:35)
at com.apps.criminalintent.CriminalIntentJSONSerializer.loadCrimes(CriminalIntentJSONSerializer.java:68)
at com.apps.criminalintent.CrimeLab.<init>(CrimeLab.java:28)
at com.apps.criminalintent.CrimeLab.get(CrimeLab.java:61)
at com.apps.criminalintent.CrimeListFragment.onCreate(CrimeListFragment.java:37)
at android.app.Fragment.performCreate(Fragment.java:1678)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5240)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

我认为你的问题来自于你的 Crime 构造函数:

mDate = new Date(json.getLong(JSON_DATE));

日期不能像长整型那样被解析。我认为你有一个 JSONException。

编辑:您可以通过节省毫秒时间来解决问题:

json.put(JSON_DATE, getmDate().getTime());

关于java - JSON 数据未加载回 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25408561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com