Home > Back-end >  Json file is not serialized properly
Json file is not serialized properly

Time:05-22

I'm trying to get data from API. My app crashes and gives me this message:

kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the array '[', but had 'EOF' instead JSON input: .....f2afd0-71e8-5229-9ac9-17d602cd35e4"}],"meta":{"hits":3934}}}

I'm facing this error since a week and I can't find out a solution for it. I need help.

This is the json rsponse:

 {"copyright":"Copyright (c) 2022 The New York Times Company. All Rights Reserved.","response":{"docs":[{"abstract":"A career in health journalism has taught me that when it comes to living well, it’s the inner workout that counts the most.","web_url":"https://www.nytimes.com/2022/03/31/well/mind/health-fitness-lessons.html","snippet":"A career in health journalism has taught me that when it comes to living well, it’s the inner workout that counts the most.","lead_paragraph":"When I first started writing about health more than 20 years ago, my columns mostly focused on the physical body: A healthy diet, exercise and screening for disease were regular topics.","source":"The New York Times","multimedia":[{"rank":0,"subtype":"xlarge","caption":null,"credit":null,"type":"image","url":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-articleLarge.jpg","height":400,"width":600,"subType":"xlarge","crop_name":"articleLarge","legacy":{"xlarge":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-articleLarge.jpg","xlargewidth":600,"xlargeheight":400}},{"rank":0,"subtype":"jumbo","caption":null,"credit":null,"type":"image","url":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-jumbo.jpg","height":682,"width":1024,"subType":"jumbo","crop_name":"jumbo","legacy":{}},{"rank":0,"subtype":"superJumbo","caption":null,"credit":null,"type":"image","url":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-superJumbo.jpg","height":1166,"width":1750,"subType":"superJumbo","crop_name":"superJumbo","legacy":{}},{"rank":0,"subtype":"thumbnail","caption":null,"credit":null,"type":"image","url":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-thumbStandard.jpg","height":75,"width":75,"subType":"thumbnail","crop_name":"thumbStandard","legacy":{"thumbnail":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-thumbStandard.jpg","thumbnailwidth":75,"thumbnailheight":75}},{"rank":0,"subtype":"thumbLarge","caption":null,"credit":null,"type":"image","url":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-thumbLarge.jpg","height":150,"width":150,"subType":"thumbLarge","crop_name":"thumbLarge","legacy":{}}],"headline":{"main":"For Better Health, Try Fitness From the Inside Out","kicker":null,"content_kicker":null,"print_headline":"","name":null,"seo":null,"sub":null},"keywords":[{"name":"subject","value":"Content Type: Service","rank":1,"major":"N"},{"name":"subject","value":"Anxiety and Stress","rank":2,"major":"N"},{"name":"subject","value":"Meditation","rank":3,"major":"N"},{"name":"subject","value":"Exercise","rank":4,"major":"N"}],"pub_date":"2022-04-01T00:00:08 0000","document_type":"article","news_desk":"Well","section_name":"Well","subsection_name":"Mind","byline":{"original":"By Tara Parker-Pope","person":[{"firstname":"Tara","middlename":null,"lastname":"Parker-Pope","qualifier":null,"title":null,"role":"reported","organization":"","rank":1}],"organization":null},"type_of_material":"News","_id":"nyt://article/0115da20-943c-52dc-8992-817faa170aa1","word_count":1235,"uri":"nyt://article/0115da20-943c-52dc-8992-817faa170aa1"},{"abstract":"He was famous for his portraits of supermodels and his close relationship with Princess Diana. His images are part of the fashion canon.","web_url":"https://www.nytimes.com/2022/03/31/style/patrick-demarchelier-dead.html","snippet":"He was famous for his portraits of supermodels and his close relationship with Princess Diana. His images are part of the fashion canon.","lead_paragraph":"Patrick Demarchelier, a photographer whose work helped define fashion and celebrity in the late 20th and early 21st centuries, died on Thursday. He was 78.","print_section":"A","print_page":"26","source":"The New York Times","multimedia":[{"rank":0,"subtype":"xlarge","caption":null,"credit":null,"type":"image","url":"images/2022/04/03/obituaries/02DEMARCHELIER-print2/00DEMARCHELIER-diana-articleLarge.jpg","height":817,"width":600,"subType":"xlarge","crop_name":"articleLarge","legacy":{"xlarge":"images/2022/04/03/obituaries/02DEMARCHELIER-print2/00DEMARCHELIER-diana-articleLarge.jpg","xlargewidth":600,"xlargeheight":817}},{"rank":0,"subtype":"jumbo","caption":null,"credit":null,"type":"image","url":"images/2022/04/03/obituaries/02DEMARCHELIER-print2/00DEMARCHELIER-diana-jumbo.jpg","height":1024,"width":752,"subType":"jumbo","crop_name":"jumbo","legacy":{}},{"rank":0,"subtype":"superJumbo","caption":null,"credit":null,"type":"image","url":"images/2022/04/03/obituaries/02DEMARCHELIER-print2/00DEMARCHELIER-diana-superJumbo.jpg","height":2048,"width":1504,"subType":"superJumbo","crop_name":"superJumbo","legacy":{}},{"rank":0,"subtype":"thumbnail","caption":null,"credit":null,"type":"image","url":"images/2022/04/03/obituaries/02DEMARCHELIER-print2/00DEMARCHELIER-diana-thumbStandard-v2.jpg","height":75,"width":75,"subType":"thumbnail","crop_name":"thumbStandard","legacy":{"thumbnail":"images/2022/04/03/obituaries/02DEMARCHELIER-print2/00DEMARCHELIER-diana-thumbStandard-.....

These are some of the data classes

    @Serializable
data class Article(
    @SerialName("copyright")
    val copyright: String?,
    @SerialName("response")
    val response: Response?
)

@Serializable
data class Response(
    @SerialName("docs")
    val docs: List<Doc>?,
    @SerialName("meta")
    val meta: Meta?
)


@Serializable
data class Doc(
    @SerialName("abstract")
    val `abstract`: String?,
    @SerialName("byline")
    val byline: Byline?,
    @SerialName("document_type")
    val documentType: String?,
    @SerialName("headline")
    val headline: Headline?,
    @SerialName("_id")
    val id: String?,
    @SerialName("keywords")
    val keywords: List<Keyword>?,
    @SerialName("lead_paragraph")
    val leadParagraph: String?,
    @SerialName("multimedia")
    val multimedia: List<Multimedia>?,
    @SerialName("news_desk")
    val newsDesk: String?,
    @SerialName("print_page")
    val printPage: String?,
    @SerialName("print_section")
    val printSection: String?,
    @SerialName("pub_date")
    val pubDate: String?,
    @SerialName("section_name")
    val sectionName: String?,
    @SerialName("snippet")
    val snippet: String?,
    @SerialName("source")
    val source: String?,
    @SerialName("subsection_name")
    val subsectionName: String?,
    @SerialName("type_of_material")
    val typeOfMaterial: String?,
    @SerialName("uri")
    val uri: String?,
    @SerialName("web_url")
    val webUrl: String?,
    @SerialName("word_count")
    val wordCount: Int?
)

This is the provide function for the retrofit instance:

    @ExperimentalSerializationApi
@Provides
@Singleton
fun provideRetrofitInstance(okHttpClient: OkHttpClient): Retrofit {
    val contentType = "application/json".toMediaType()
    return Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(json.asConverterFactory(contentType))
        .build()
}

CodePudding user response:

you should be using this plugin

JSON To Kotlin Class ​(JsonToKotlinClass)

this plugin makes your work easy

after installing

and paste all JSON

before using read all documents New > Kotlin data class

CodePudding user response:

I recommend using Gson Or moshi to Convert Json Response.

  1. Add dependency in build.Gradle (moshi or gson)
  2. configure retrofit .addConverterFactory(MoshiConverterFactory.create(moshi)) for moshi
  3. Annotate data class with @Json for moshi or @SerializableName for Gson.

Hope it helps !

  • Related