重複の可能性:
GSON が「BEGIN_OBJECT が必要ですが、BEGIN_ARRAY でした」をスローしますか?
オブジェクトを Android に保存しようとしていますが、読み取りに問題があります
私のオブジェクト
public class User {
private String login;
private String password;
public User(String login, String password){
this.login = login;
this.password = password;
}
public String getLogin(){
return this.login;
}
}
オブジェクトを内部メモリ デバイスに保存します
String FILENAME = getString(R.string.store);
User storeUser = new User(user.getText().toString(), password.getText().toString());
Gson gson = new Gson();
String data = gson.toJson(storeUser);
JSONArray mJSONArray = new JSONArray(Arrays.asList(data));
String string = mJSONArray.toString();
FileOutputStream fos;
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
そして読んでみる
String FILENAME = getString(R.string.store);
FileInputStream fis;
fis = openFileInput(FILENAME);
InputStreamReader inputStreamReader = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
System.out.println(sb.toString());
Gson gson = new Gson();
User user = gson.fromJson(sb.toString(), User.class);
System.out.println(user.getLogin());
そして、私が持っています
01-12 14:06:36.703: I/System.out(30366): [" {\"login\":\"username2\",\"password\":\"password\"}"]
01-12 14:06:36.753: D/AndroidRuntime(30366): Shutting down VM
01-12 14:06:36.753: W/dalvikvm(30366): threadid=1: thread exiting with uncaught exception (group=0x400205a0)
01-12 14:06:36.753: E/AndroidRuntime(30366): FATAL EXCEPTION: main
01-12 14:06:36.753: E/AndroidRuntime(30366): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
01-12 14:06:36.753: E/AndroidRuntime(30366): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
01-12 14:06:36.753: E/AndroidRuntime(30366): at com.google.gson.Gson.fromJson(Gson.java:795)
01-12 14:06:36.753: E/AndroidRuntime(30366): at com.google.gson.Gson.fromJson(Gson.java:761)
01-12 14:06:36.753: E/AndroidRuntime(30366): at com.google.gson.Gson.fromJson(Gson.java:710)
01-12 14:06:36.753: E/AndroidRuntime(30366): at com.google.gson.Gson.fromJson(Gson.java:682)
01-12 14:06:36.753: E/AndroidRuntime(30366): at ant.junior.geoloc.MainActivity$1.onClick(MainActivity.java:65)
01-12 14:06:36.753: E/AndroidRuntime(30366): at android.view.View.performClick(View.java:2532)
01-12 14:06:36.753: E/AndroidRuntime(30366): at android.view.View$PerformClick.run(View.java:9293)
01-12 14:06:36.753: E/AndroidRuntime(30366): at android.os.Handler.handleCallback(Handler.java:587)
01-12 14:06:36.753: E/AndroidRuntime(30366): at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 14:06:36.753: E/AndroidRuntime(30366): at android.os.Looper.loop(Looper.java:150)
01-12 14:06:36.753: E/AndroidRuntime(30366): at android.app.ActivityThread.main(ActivityThread.java:4277)
01-12 14:06:36.753: E/AndroidRuntime(30366): at java.lang.reflect.Method.invokeNative(Native Method)
01-12 14:06:36.753: E/AndroidRuntime(30366): at java.lang.reflect.Method.invoke(Method.java:507)
01-12 14:06:36.753: E/AndroidRuntime(30366): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-12 14:06:36.753: E/AndroidRuntime(30366): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-12 14:06:36.753: E/AndroidRuntime(30366): at dalvik.system.NativeStart.main(Native Method)
01-12 14:06:36.753: E/AndroidRuntime(30366): Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
01-12 14:06:36.753: E/AndroidRuntime(30366): at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
01-12 14:06:36.753: E/AndroidRuntime(30366): at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
01-12 14:06:36.753: E/AndroidRuntime(30366): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
01-12 14:06:36.753: E/AndroidRuntime(30366): ... 16 more
ありがとう