google / gson

A Java serialization/deserialization library to convert Java Objects into JSON and back
Apache License 2.0
23.23k stars 4.27k forks source link

Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ #966

Closed LucianWang closed 7 years ago

LucianWang commented 7 years ago

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.Gson.fromJson(Gson.java:826) at com.google.gson.Gson.fromJson(Gson.java:779) at com.google.gson.Gson.fromJson(Gson.java:728) at readtv.ghs.tv.util.RewardRuleUtil$1.onSuccess(RewardRuleUtil.java:63) at readtv.ghs.tv.http.AsyncHttpClient$2$2.run(AsyncHttpClient.java:116) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5013) 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:811) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:627) Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60) at com.google.gson.Gson.fromJson(Gson.java:814) ... 12 more java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60) at com.google.gson.Gson.fromJson(Gson.java:814) at com.google.gson.Gson.fromJson(Gson.java:779) at com.google.gson.Gson.fromJson(Gson.java:728) at readtv.ghs.tv.util.RewardRuleUtil$1.onSuccess(RewardRuleUtil.java:63) at readtv.ghs.tv.http.AsyncHttpClient$2$2.run(AsyncHttpClient.java:116) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5013) 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:811) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:627)

Often this kind of problem, I hope you can tell me how to deal with the simple. Or where it is. I don't think it's our problem.

JakeWharton commented 7 years ago

The exception indicates the problem. The type you asked Gson to deserialize was a list or array but the JSON contained a string or wasn't JSON at all (and thus appeared like a string).

On Sun, Nov 27, 2016, 3:04 AM LucianWang notifications@github.com wrote:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.Gson.fromJson(Gson.java:826) at com.google.gson.Gson.fromJson(Gson.java:779) at com.google.gson.Gson.fromJson(Gson.java:728) at readtv.ghs.tv.util.RewardRuleUtil$1.onSuccess(RewardRuleUtil.java:63) at readtv.ghs.tv.http.AsyncHttpClient$2$2.run(AsyncHttpClient.java:116) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5013) 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:811) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:627) Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60) at com.google.gson.Gson.fromJson(Gson.java:814) ... 12 more java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60) at com.google.gson.Gson.fromJson(Gson.java:814) at com.google.gson.Gson.fromJson(Gson.java:779) at com.google.gson.Gson.fromJson(Gson.java:728) at readtv.ghs.tv.util.RewardRuleUtil$1.onSuccess(RewardRuleUtil.java:63) at readtv.ghs.tv.http.AsyncHttpClient$2$2.run(AsyncHttpClient.java:116) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5013) 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:811) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:627)

Often this kind of problem, I hope you can tell me how to deal with the simple. Or where it is. I don't think it's our problem.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/gson/issues/966, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEZ7Q9ENsJiHfxUpQMEVlZRWlsbk6ks5rCTmhgaJpZM4K9FyN .

LucianWang commented 7 years ago

Thank you.

SiddarthG commented 6 years ago

Hi I have the same problem and this happened all of a sudden previously it was working fine,i'm having a real hard time analyzing what has changed that led to this problem,can someone please have a look at this and help me out,

if the formatting here is not clear, i have posted in stackoverflow https://stackoverflow.com/questions/48840747/onfailure-methods-throws-exception-expected-begin-array-but-was-string-at-line

I have this Json Response coming from server:

[{ "disposal_type_category": { "category_entry_total": "21" }, "disposal_types": [{ "ID": "78", "entries": [{ "ID": 2584, "entry_tally": "1" }, { "ID": 2578, "entry_tally": "1" } ] }, { "ID": "1323", "entries": [{ "ID": 2583, "entry_tally": "1" }, { "ID": 2579, "entry_tally": "1" } ] } ] }, { "disposal_type_category": { "category_entry_total": "25.2" }, "disposal_types": [{ "ID": "80", "entries": [] }, { "ID": "84", "entries": [] } ] } ]

This is in my ApiClient.java:

public static Retrofit getClient() { if (retrofit==null) { Gson gson = new GsonBuilder().setLenient().create(); retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); } return retrofit; }

This is in ApiInterface.java:

Call<List<Disposal>> getCollection(@Header("Authorization") String token);

Disposal.java:

public class Disposal { @SerializedName("disposal_type_category") @Expose public DisposalTypeCategory disposalTypeCategory; @SerializedName("disposal_types") @Expose public List<DisposalType> disposalTypes = null; }

DisposalTypeCategory.java:

public class DisposalTypeCategory { @SerializedName("category_entry_total") @Expose public String categoryEntryTotal; }

DisposalType.java:

public class DisposalType { @SerializedName("ID") @Expose public String iD; @SerializedName("entries") @Expose public List<Entries> entries = null; }

Entries.java:

public class Entries { @SerializedName("ID") @Expose private int iD; }

I have set setLineant(true) for Gson because if i remove it i will get malformed json exception, even this issue started recently previously was fine, not sure if its related. The issue started after 2-3 new fields were added to API and the base url was changed but still the core remains same.

kalyakiplangat commented 6 years ago

how do you solve this problem

SiddarthG commented 6 years ago

@kalyakiplangat for me checking everything again with the backend developer and seeing if any API response is returning something not as expected by the app. There were changes made in return format of the API response by backend Dev which I wasn't aware of..so I was expecting string in app but API was sending array

uniquelipu commented 6 years ago

I have not declared JSON or Gson anywhere , but why I'm getting error

acarolsf commented 6 years ago

@uniquelipu try to clean the project, you can go build >> clean project, for me, it worked

Godslight001 commented 6 years ago

Cleaning the project worked for me

mak0501 commented 6 years ago

Cleaning the project worked for me as well

ski96277 commented 6 years ago

Clean the Project

BarrackRonald commented 6 years ago

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

I got a similar error message but cleaning the app did the trick for me

junglesafari commented 6 years ago

it worked for me too..

J1015 commented 6 years ago

cleaning and rebuilding the project helped me to solve this problem...

imgauravanimator commented 6 years ago

for me too.

david14ka commented 6 years ago

for me too

yasincidem commented 5 years ago

For me the problem was this one -> java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 12 column 11 path $.items[0].id

Changing @SerializedName("id") val id: Id to @SerializedName("id") val id: String worked.

MisterAwesome23 commented 5 years ago

If someone is getting this error in AndroidStudio : Try two things:

  1. Roll back to last working conditions. (Revert if you use VCS).
  2. In build options Clean project and rebuild. (Worked for me.)

I'm fairly new to android. Excuse any mistakes if committed. Suggestions are welcome :)

juniorjoe567 commented 5 years ago

Please go to Build, then clean. This option worked for me.

MisterAwesome23 commented 5 years ago

Please go to Build, then clean. This option worked for me.

Happy learning :)

Purvik commented 5 years ago

After cleaning project and rebuilding, still I'm having issue with the same error. Don' t know how to resolve as i already checked all parameter naming conventions not once but thrice.

hesamaldinahani commented 5 years ago

Thanks bro, cleaning project worked for me

mirrorsh0w commented 5 years ago

@uniquelipu try to clean the project, you can go build >> clean project, for me, it worked

whoa it worked thanks for sharing.

saddahussain commented 5 years ago

Rebuilding the project will fix the issue.

zionnite commented 5 years ago

cleaning and rebuilding did the trick

jonssfgshds commented 5 years ago

thanks alot cleaning the project is remedy for the error

tovidd commented 5 years ago

i got this: Expected BEGIN_OBJECT but was STRING at line 1 column 2

i solve by: you need an object "Class", but you got List<"Class"> so your API need to return response using $data: Class::find()->first(); dont use get() or all();

Nikoloutsos commented 5 years ago

I got this problem when making some changes to square calendar widget. But thankfully, I solved it by cleaning and rebuilding the project! I hate these errors that come out of blue without reason ;/

tariqnz commented 5 years ago

cleaning project and rebuilding worked for me as well

mashihab commented 4 years ago

Cleaning the project worked for me

VidyaCK commented 4 years ago

gradlew clean worked for me.

yashvenrakumar commented 4 years ago

plz suggest the solution of the error

VidyaCKabber commented 4 years ago

Here are two answer on stack overflow for the same type of issue. Plz, have a look once.

https://stackoverflow.com/questions/28418662/expected-begin-object-but-was-string-at-line-1-column-1

https://stackoverflow.com/questions/31424372/json-error-java-lang-illegalstateexception-expected-begin-object-but-was-strin/31425418

ilhamfidatama commented 4 years ago

clean project and rebuild project doesn't work for my issue https://stackoverflow.com/questions/60568620/retrofit2-get-method-failure-response

sabrimev commented 4 years ago

It's weird but I am still getting this error even did everything listed on the web. What my case is I am trying to update the app over the existing one. They both separately working well without error but when I try to update latest version over the previous one I am receiving the following exception;

java.lang.IllegalStateException: Expected an int but was BEGIN_ARRAY at line 1 column 99 path $[0].e

There is no error in the retrieved Json file. The funny thing is when I revert my code all working well. And in that code I am using sorting like comparable and comparator over serialized class.

Vinodsharma123 commented 4 years ago

Cleaning and rebuiding the project didnt work for me! iam getting the same exception again and again

nikunjparadva commented 4 years ago

First :- clean and rebuild

if it not worked then

Second:- Just delete previous release apk folder

adtoba commented 4 years ago

Cleaning the app did it for me

Vinodsharma123 commented 4 years ago

Thanks

On Wed, 24 Jun 2020, 8:21 pm Adebisi Adetoba, notifications@github.com wrote:

Cleaning the app did it for me

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/google/gson/issues/966#issuecomment-648869280, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGK3TMPFS464OKZPIQBNTILRYIHF7ANCNFSM4CXULSGQ .

rishabh547 commented 4 years ago

@uniquelipu try to clean the project, you can go build >> clean project, for me, it worked

i am not getting option of clean project in build dropdown? How to enable it

rishabh547 commented 4 years ago

First :- clean and rebuild

if it not worked then

Second:- Just delete previous release apk folder

i am not getting option of clean project in build dropdown? How to enable it

rishabh547 commented 4 years ago

First :- clean and rebuild

if it not worked then

Second:- Just delete previous release apk folder

i am not getting option of clean project in build dropdown? How to enable it

raghav88715 commented 3 years ago

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

I got a similar error message but cleaning the app did the trick for me

how to clean the app?

breenbob commented 3 years ago

Updating the version of Gradle I target fixed this for me when clean/rebuild would not when receiving this as a project build error in Android Studio.

andybest27 commented 3 years ago

Clear the project and rebuild tmp-cam-7806164340706841378

rjaydeep commented 3 years ago

not resolve issue after cleaning project

Kayuemkhan commented 3 years ago

still getting Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

onyashed commented 3 years ago

Google should deprecate or abandon this gson structure. It is a time stealer. Developers waste alot of precious time on this bug or error or design flaw. My opinion. We should work best with json.

faheemezani commented 2 years ago

still getting Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

I encountered the same problem and discovered that, in my case, the server was responding with a compressed format (gzip). An interceptor was added to my OkHttpClient to decompress the gzipped format response. I had referred to the following post: okhttp 3: how to decompress gzip/deflate response manually using Java/Android

XavierGomez95 commented 2 years ago

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.Gson.fromJson(Gson.java:826) at com.google.gson.Gson.fromJson(Gson.java:779) at com.google.gson.Gson.fromJson(Gson.java:728) at readtv.ghs.tv.util.RewardRuleUtil$1.onSuccess(RewardRuleUtil.java:63) at readtv.ghs.tv.http.AsyncHttpClient$2$2.run(AsyncHttpClient.java:116) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5013) 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:811) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:627) Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60) at com.google.gson.Gson.fromJson(Gson.java:814) ... 12 more java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $ at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:79) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60) at com.google.gson.Gson.fromJson(Gson.java:814) at com.google.gson.Gson.fromJson(Gson.java:779) at com.google.gson.Gson.fromJson(Gson.java:728) at readtv.ghs.tv.util.RewardRuleUtil$1.onSuccess(RewardRuleUtil.java:63) at readtv.ghs.tv.http.AsyncHttpClient$2$2.run(AsyncHttpClient.java:116) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5013) 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:811) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:627)

Often this kind of problem, I hope you can tell me how to deal with the simple. Or where it is. I don't think it's our problem.

In my case it was an error in the Json. The API returned a String instead of a List.

Need: ["message one", "message two", "message three"]. Get: "["message one", "message two", "message three"]"

In case that's what it is, get in touch with the Backend partner to fix the problem.

I hope the answer will help you :)