skip to Main Content

I am developing an Android application using Firebase FireStore Database.

When my application read data, Firebase returns below Json data.

{[email protected], name=DennisNaver, comment=Test profile for Dennis Naver account, imageUrl=None}

And I am using below Kotlin code to parse Json data.

val profile = Gson().fromJson(task.result.data.toString(), Profile::class.java)

Then Gson throw below error message.

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 60 path $.comment

The data class Profile is defined as below.

data class Profile(var imageUrl: String, var email: String, var name: String, var comment: String)

I modified Json data as below and tried parse it using Gson. Then it worked.

{email=“[email protected]”, name=“DennisNaver”, comment=“Test profile for Dennis Naver account”, imageUrl=“None”}

How can I fix is issue?

2

Answers


  1. Chosen as BEST ANSWER

    This is self answer. I fixed this issue by using both Gson().toJson() & Gson().fromJson()

    val json = Gson().toJson(task.result.data)
    val profile = Gson().fromJson(json, Profile::class.java)
    
    

  2. You need to fix your data. The initial version is not JSON. JSON requires the quotes. Without those, it’s impossible for the parser to know where one value ends and the next begins. I don’t know what put the data into your db, but that process is broken.B

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search