可能重复:
GSON throwing “Expected BEGIN_OBJECT but was 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
由于
答案 0 :(得分:0)
好的,我发现错误,编码2次。