我正在尝试序列化一个ArrayList,以便在我关闭并重新启动我的App时,它可以与所有Claim Attributes一起使用。我收到标题中所示的错误。预期的begin_array但获得了begin_object。

这是我的代码:

    @Override
protected void onStart(){
    super.onStart();
    claim = loadFromFile();
    Toast.makeText(this, "loading File"+claim.size(), Toast.LENGTH_SHORT).show();
    ClaimsList cl = new ClaimsList();
    cl.setClaimList(claim);
    //ArrayAdapter<Claim> claimAdapter = new ArrayAdapter<Claim>(this, R.layout.custom_view_claim, claim);
    //listView.setAdapter(claimAdapter);
}

public void saveInFile(Claim claim) {
    Gson gson = new Gson();
    try {
        FileOutputStream fos = openFileOutput(this.SAVEFILE,0);
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        gson.toJson(claim, osw);
        osw.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

//recover those persistent data created in the save function
private ArrayList<Claim> loadFromFile() {
    Gson gson = new Gson();
    ArrayList<Claim> claim = new ArrayList<Claim>();
    try {
        FileInputStream fis = openFileInput(SAVEFILE);
        //Based on http://google.gson.googlecode.com/svn/trunk/gson/dos/javadoc/com/google/gson/Gson.html
        Type listType = new TypeToken<ArrayList<Claim>>(){}.getType();
        InputStreamReader isr = new InputStreamReader(fis);
        claim = gson.fromJson(isr, listType);
        fis.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (claim == null){
        claim = new ArrayList<Claim>();
    }
    return claim;
}


以下是我的班级贡献者:

ArrayList<Claim> claim;
String SAVEFILE = "file.sav";


最后是logCat:

    02-01 16:50:45.121: E/AndroidRuntime(4695): FATAL EXCEPTION: main
02-01 16:50:45.121: E/AndroidRuntime(4695): Process: app.zioueche_travelexpense, PID: 4695
02-01 16:50:45.121: E/AndroidRuntime(4695): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.zioueche_travelexpense/app.zioueche_travelexpense.AddClaim}: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
02-01 16:50:45.121: E/AndroidRuntime(4695):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at android.app.ActivityThread.access$900(ActivityThread.java:161)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at android.os.Looper.loop(Looper.java:157)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at android.app.ActivityThread.main(ActivityThread.java:5356)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at java.lang.reflect.Method.invokeNative(Native Method)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at java.lang.reflect.Method.invoke(Method.java:515)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at dalvik.system.NativeStart.main(Native Method)
02-01 16:50:45.121: E/AndroidRuntime(4695): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
02-01 16:50:45.121: E/AndroidRuntime(4695):     at com.google.gson.Gson.fromJson(Gson.java:822)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at com.google.gson.Gson.fromJson(Gson.java:775)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at app.zioueche_travelexpense.AddClaim.loadFromFile(AddClaim.java:273)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at app.zioueche_travelexpense.AddClaim.onStart(AddClaim.java:239)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1189)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at android.app.Activity.performStart(Activity.java:5436)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
02-01 16:50:45.121: E/AndroidRuntime(4695):     ... 11 more
02-01 16:50:45.121: E/AndroidRuntime(4695): Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
02-01 16:50:45.121: E/AndroidRuntime(4695):     at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
02-01 16:50:45.121: E/AndroidRuntime(4695):     at com.google.gson.Gson.fromJson(Gson.java:810)

最佳答案

您可以使用以下功能:isJsonObject()isJsonArray()

例如:

     Gson gson = new Gson();
            JsonParser parser = new JsonParser();
             JsonElement jsonElement = parser.parse(productJSONStr);



            if (jsonElement.isJsonArray()) {
            }
            else if (jsonElement.isJsonObject()) {

            }else{

            }


希望对您有所帮助。

关于java - 预期为BEGIN_ARRAY,但已为BEGIN_OBJECT Gson android可序列化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28269353/

10-10 15:05