简体   繁体   中英

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

Json of this kind comes to postman

{
"message": "The given data was invalid.",
"errors": {
    "phone": [
        "The phone has already been taken."
    ]
}

When prompted in the code, the error "java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $" appears

Here are my models

data class Response(
        @SerializedName(MESSAGE)
        val reason: String,

        @SerializedName(ERRORS)
        val errors: Errors? = null
    ) {
        companion object {
            private const val MESSAGE = "message"
            private const val ERRORS = "errors"    }
    }

data class Errors(

    @SerializedName("phone")
    val phone: List<String?>? = null
)

When I add a phone number that is not already on my account, I receive such a json

{
    "message": "Successfully added mobile!"
}

Create a two files Response.kt and Errors.kt Inside Response.kt

import com.google.gson.annotations.SerializedName

data class Response(
    @SerializedName("errors")
    val errors: Errors?,
    @SerializedName("message")
    val message: String?
)

Inside Errors.kt

import com.google.gson.annotations.SerializedName

data class Errors(
    @SerializedName("phone")
    val phone: List<String?>?
)

If Errors are not received in JSON means Please check null. If not working Please your checkresponse format.Hope it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Retrofit and gson Error : java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ Gson: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 184 path $.data com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3 path $[0] Gson Error Retrofit Error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $ com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 2 column 2 path $ while UPDATE com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 39 path $.message com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3 path $[0] com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 101 path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM