0

I am storing an arraylist of Objects in JSON format in a file. As soon as new arraylist appears it converts the object in JSON format and append it in the file.The encoding works fine. But while decoding it throws exception. I am quite new in this field and learning.Any help is welcome.

Encoding Code

public static void jsondumpfile(ArrayList<HtmlElementObject> sanitizelog)
{
    try {
        File file = new File("c:\\temp\\auditinfojson.json");

        if(!file.exists()){

            file.createNewFile();
        }


        FileWriter fileWriter = new FileWriter(file,true);
        BufferedWriter bw = new BufferedWriter(fileWriter);
        Gson gson=new Gson();
        //bw.append("---------------");
            //bw.append(gson.toJson(sanitizelog));
        //fw.append(gson.toJson(sanitizelog));
        for(HtmlElementObject eachobj : sanitizelog)

        {

             bw.write(gson.toJson(eachobj));

         }



        //bw.flush();
        bw.close();
    logElementData.clear();


    } catch (IOException e) {
        // TODO: handle exception
        e.printStackTrace();
    }



}

RESULTANT FILE AFTER ENCODING {"appLoginId":1058,"tabId":"1","elementType":"Image","label":"No Image Name","value":"https://admin.xyz.com","seqTrail":"No possible trail sequence","timeStamp":"2014-01-31 13:02:42.618"} {"appLoginId":1058,"tabId":"1","elementType":"Image","label":"No Image Name","value":"https://admin.xyz.com/xyz/images/btn-cancel.gif","seqTrail":"No possible trail sequence","timeStamp":"2014-01-31 13:02:42.625"}

Like this multiple objects are stored.

DECODING/PARSING BACk CODE

public static void extractfromjson() throws IOException
 {



   ArrayList<HtmlElementObject> tCollection = new ArrayList<HtmlElementObject>();
   Gson gson = new Gson();
   BufferedReader bufferedReader = new BufferedReader(new FileReader(
           "c:\\temp\\auditinfojson.json"));
   Type type = new TypeToken<ArrayList<HtmlElementObject>>(){}.getType();
   ArrayList<HtmlElementObject> J_tweet = (ArrayList<HtmlElementObject>)gson.fromJson(bufferedReader, type);
   System.out.println(J_tweet);

    }

EXCEPTION THROWN **com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2** This comes when i want to retrieve the data.

3
  • 1
    I think problem is you have written HtmlElementObject as one by one json object to file, But while reading your reading as ArrayList. Either you have to Write as ArrayList and read as ArrayList OR write one by one object and read them one by one. May be to read them one by one. Commented Jan 31, 2014 at 9:30
  • Jayasagar is correct. You're writing sequential objects to a file, you're not writing an array to file. Commented Jan 31, 2014 at 9:33
  • now I am doing ` while(true){ String str=bufferedReader.readLine(); if(str==null) break; HtmlElementObject J_tweet = ((HtmlElementObject)gson.fromJson(str, type)); tCollection.add(J_tweet); } return tCollection;` but in above code since bufferedreader is string type and in myjson resultant apploginid is long type so while returning my object in applogin field it returns long not its value. else the whole object returning is correct. Commented Jan 31, 2014 at 11:05

1 Answer 1

1

When we have a JSON, the GSON fails to parse this in to Java class which has field of type List class, When JSON data comes with matches as “Object” format, it throws exception “Expected BEGIN_ARRAY but was BEGIN_OBJECT”.

This guy, https://github.com/sacdroid did a Java adapter to Fix it problem.

You can use these classes bellow, then while constructing GSON instance, you have to assign TypeAdapterFactory with ArrayAdapterFactory to GsonBuilder.

Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).create();

ArrayAdapter.java http://sachinpatil.com/downloads/code/ArrayAdapter.java

ArrayAdapterFactory.java http://sachinpatil.com/downloads/code/ArrayAdapterFactory.java


PS: In this case, you had problem with you JSON, your JSON content wasn't a Valid, I fix it putting [ ] (brackets) between the { }'s and ,(comma) to separate :

[{
    "appLoginId": 1058,
    "tabId": "1",
    "elementType": "Image",
    "label": "No Image Name",
    "value": "https://admin.xyz.com",
    "seqTrail": "No possible trail sequence",
    "timeStamp": "2014-01-31 13:02:42.618"
}, {
    "appLoginId": 1058,
    "tabId": "1",
    "elementType": "Image",
    "label": "No Image Name",
    "value": "https://admin.xyz.com/xyz/images/btn-cancel.gif",
    "seqTrail": "No possible trail sequence",
    "timeStamp": "2014-01-31 13:02:42.625"
}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.