com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 2 path

Home / Uncategorized / com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 2 path

Question:
i have tries many suggestions from other same questions but does not help, how can i run this test successfully:@Test
fun isJsonCorrectPersonConvert() {
val gson = GsonBuilder().create()
val json = gson.toJson("[{"Id":2,"Text":"Математика"},{"Id":5,"Text":"Физика"}]")
val results = gson.fromJson(json, Array::class.java)
Assert.assertNotNull(results)
Assert.assertNotNull(results[0].Id)
Assert.assertNotNull(results[0].Text)
System.out.println(results.toString())
}

data class Person(val Id: Int,val Text:String)


Answer:
Try this :val json: String = "[{"Id":2,"Text":"Математика"},{"Id":5,"Text":"Физика"}]"
val results: List = gson.fromJson(json , Array::class.java).toList()
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *