最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Kotlin Project JSON malformed with GSON converter - Stack Overflow

programmeradmin4浏览0评论

I use GsonConverter in an Android Application to convert a Json placed in a Gitlab repository. This is for a future project I would made.

The Json I made is like that :

[
    {
       "name":{
          "common":"South Geia",
          "official":"South Geia and the South Sandwich Islands"
       },
       "flags":{
          "png":".png",
          "svg":".svg"
       }
    },
    {
       "name":{
          "common":"Grenada",
          "official":"Grenada"
       },
       "flags":{
          "png":".png",
          "svg":".svg",
          "alt":"The flag of Grenada features a large central rectangular area surrounded by a red border, with three five-pointed yellow stars centered on the top and bottom borders. The central rectangle is divided diagonally into four alternating triangular areas of yellow at the top and bottom and green on the hoist and fly sides, and a five-pointed yellow star on a red circle is superimposed at its center. A symbolic nutmeg pod is situated on the green hoist-side triangle."
       }
    },
    {
       "name":{
          "common":"Switzerland",
          "official":"Swiss Confederation"
       },
       "flags":{
          "png":".png",
          "svg":".svg",
          "alt":"The flag of Switzerland is square shaped. It features a white Swiss cross centered on a red field."
       }
    }
 ] 

Parsing :

fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit = Retrofit.Builder()
    .client(okHttpClient)
    .addConverterFactory(GsonConverterFactory.create())
    .baseUrl(BASE_URL)
    .build()
    

I have created data model

@Serializable
data class WSCountry(
    @SerialName(value = "name")
    val name: WSCountryName? = null,
    @SerialName(value = "flags")
    val flags: WSFlags? = null
)
@Serializable
data class WSCountryName(
    @SerialName(value = "common")
    val common: String? = null,
    @SerialName(value = "official")
    val official: String? = null
)
@Serializable
data class WSFlags(
    @SerialName(value = "png")
    val png: String? = null,
    @SerialName(value = "svg")
    val svg: String? = null
)

When I build my project I receive an error from Gson converter :

com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 6 column 1 path $

Even if I place the setLenient(true) I receive a json expected begin_array but was string error. Could you tell me what is wrong with my json ?

Thanks

I use GsonConverter in an Android Application to convert a Json placed in a Gitlab repository. This is for a future project I would made.

The Json I made is like that :

[
    {
       "name":{
          "common":"South Geia",
          "official":"South Geia and the South Sandwich Islands"
       },
       "flags":{
          "png":"https://flagcdn/w320/gs.png",
          "svg":"https://flagcdn/gs.svg"
       }
    },
    {
       "name":{
          "common":"Grenada",
          "official":"Grenada"
       },
       "flags":{
          "png":"https://flagcdn/w320/gd.png",
          "svg":"https://flagcdn/gd.svg",
          "alt":"The flag of Grenada features a large central rectangular area surrounded by a red border, with three five-pointed yellow stars centered on the top and bottom borders. The central rectangle is divided diagonally into four alternating triangular areas of yellow at the top and bottom and green on the hoist and fly sides, and a five-pointed yellow star on a red circle is superimposed at its center. A symbolic nutmeg pod is situated on the green hoist-side triangle."
       }
    },
    {
       "name":{
          "common":"Switzerland",
          "official":"Swiss Confederation"
       },
       "flags":{
          "png":"https://flagcdn/w320/ch.png",
          "svg":"https://flagcdn/ch.svg",
          "alt":"The flag of Switzerland is square shaped. It features a white Swiss cross centered on a red field."
       }
    }
 ] 

Parsing :

fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit = Retrofit.Builder()
    .client(okHttpClient)
    .addConverterFactory(GsonConverterFactory.create())
    .baseUrl(BASE_URL)
    .build()
    

I have created data model

@Serializable
data class WSCountry(
    @SerialName(value = "name")
    val name: WSCountryName? = null,
    @SerialName(value = "flags")
    val flags: WSFlags? = null
)
@Serializable
data class WSCountryName(
    @SerialName(value = "common")
    val common: String? = null,
    @SerialName(value = "official")
    val official: String? = null
)
@Serializable
data class WSFlags(
    @SerialName(value = "png")
    val png: String? = null,
    @SerialName(value = "svg")
    val svg: String? = null
)

When I build my project I receive an error from Gson converter :

com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 6 column 1 path $

Even if I place the setLenient(true) I receive a json expected begin_array but was string error. Could you tell me what is wrong with my json ?

Thanks

Share Improve this question edited Mar 18 at 9:22 Nicolas DUBOSCQ asked Mar 18 at 8:33 Nicolas DUBOSCQNicolas DUBOSCQ 551 silver badge6 bronze badges 4
  • 2 Where is the Gson parsing part? share that code as well. – Usman Rana Commented Mar 18 at 9:11
  • Just edit my question with retrofit/gson parsing – Nicolas DUBOSCQ Commented Mar 18 at 9:23
  • Also add how you are calling the api and using GSON – Taranmeet Singh Commented Mar 18 at 12:30
  • also for Gson parsing, the annotation should be @SerializedName instead of SerialName – Usman Rana Commented Mar 19 at 9:33
Add a comment  | 

1 Answer 1

Reset to default 0

In my case, I was facing the same issue so I decided to replace this converter with another one that I created on my own with JSON object from Kotlinx serialization library cause I think retrofit doesn't support the serializer you are using on your data class name and property so you have to create a JSON converter on your own as I do and this solution works for me:

Here is how I've implemented it:

private val contentType = "application/json".toMediaType()
private val json = Json {
        prettyPrint = true
        ignoreUnknownKeys = true
    }


val retrofit = Retrofit.Builder()
    .addConverterFactory(json.asConverterFactory(contentType))
    .baseUrl(BASE_URL)
    .client(okHTTPClient)
    .build()

You can also add a client :

private val okHTTPClient = OkHttpClient.Builder()
    .readTimeout(60, TimeUnit.SECONDS)
    .connectTimeout(60, TimeUnit.SECONDS)
    .writeTimeout(60, TimeUnit.SECONDS)
    .addInterceptor(logging)
    .build()

And an interceptor, if you want to understand more what is happening when the request is processed:

private val logging = HttpLoggingInterceptor().apply {
    setLevel(HttpLoggingInterceptor.Level.BODY)
}
发布评论

评论列表(0)

  1. 暂无评论