This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Reason for exception

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.Gson.fromJson(Gson.java:944)
at com.google.gson.Gson.fromJson(Gson.java:897)
at com.google.gson.Gson.fromJson(Gson.java:846)
at com.onedevice.admin.MainActivity$15.onResponse(MainActivity.java:886)
at com.onedevice.admin.RequestNetworkController$3$2.run(RequestNetworkController.java:179)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6748)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:384)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:183)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
at com.google.gson.Gson.fromJson(Gson.java:932)
... 11 more
 
Marshal
Posts: 80665
478
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid we can't help from the information you have given us. Please supply lots more details. I have moved you to what I think is a more appropriate location. The stack trace mentioned Android, so I moved you to that forum; I don't know any Android myself.
 
Sheriff
Posts: 28401
100
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is that the JSON you're trying to process is badly formed. So that JSON should be included in the details you post next.
 
Marshal
Posts: 4796
601
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:My guess is that the JSON you're trying to process is badly formed.


I agree - the message Expected BEGIN_OBJECT but was STRING is telling you that something other than an opening curly-bracket was found when trying to parse an object.
 
Ron McLeod
Marshal
Posts: 4796
601
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are getting the content from a network endpoint:
  - check to the return status to ensure that the request was processed properly (for example - with HTTP: a 200 response)
  - ensure that the content type provided that the endpoint was as-expected (for example - with HTTP: Content-Type: application/json)
  - ensure that the content length is greater than zero

It would also be helpful to log the actual content so that you can visually inspect what was returned (or if more complex, copy/paste it in to an on-line JSON parser such as JSON Formatter.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Things to check quickly:
1. If you are using Gson, Kotlinx-serialization... basically, anything that is not the bad old JSONObject, make sure that it is configured to skip missing/redundant fields.
2. Did you try to just copy-paste your JSON to some JSON-lint site? https://jsonlint.com/ Note that when copy-pasting the JSON, Android Studio makes a mess with the quotes.
3. Alternatively, did you try to parse it with JSONObject(json) just to make sure it's legal.
That could be a hint.

Where do you go to from here?
1. If the JSON is legal, just make sure your parser can eat missing/redundant fields.
2. If not - the Achiles Heel is always the quotes. People forget to add them, or add them wrong.
3. If you are using KTOR or Retrofit - make sure that the headers are application/json. I've seen cases where it's fed wrong.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This error show that you have JsonSyntaxException error. You are seeing  (‘Expected BEGIN_OBJECT but was STRING’) and this happens when the parser expects a JSON object unfortunately receives a plain string.
If you want to validate and quick check your JSON file, check on the two website recommended or from here as well Pretty Json. All of these tool will help you to see the problem.
reply
    Bookmark Topic Watch Topic
  • New Topic