如何正确序列化包含字符串数组的JSON文件。

huangapple go评论149阅读模式
英文:

How do I properly serialize a JSON file that contains an array of strings

问题

我有这个字符串数组:

["https://cdn.shibe.online/shibes/3f47b1c848763f156778015a20da91b5f365ea93.jpg","https://cdn.shibe.online/shibes/22ee83228be55bce16bc8c6e91790700229892b9.jpg"]

我尝试这样做,但它不起作用。

@Serializable
data class ShibePhoto(

    @SerialName("photos")
    val photos: List<String>
)

我得到这个错误:

kotlinx.serialization.json.internal.JsonDecodingException: 偏移0处的意外JSON令牌:预期对象的开头'{',但实际是'[',路径:$[0]

英文:

I have this array of strings:
[&quot;https://cdn.shibe.online/shibes/3f47b1c848763f156778015a20da91b5f365ea93.jpg&quot;,&quot;https://cdn.shibe.online/shibes/22ee83228be55bce16bc8c6e91790700229892b9.jpg&quot;]

I'm trying to do this, but it's not working.

@Serializable
data class ShibePhoto(

    @SerialName(&quot;photos&quot;)
    val photos: List&lt;String&gt;
)

I'm getting this error

kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 0: Expected start of the object &#39;{&#39;, but had &#39;[&#39; instead at path: $[0]

答案1

得分: 0

你基本上是在说响应的格式是这样的

{&quot;photos&quot;:[...]}

而不仅仅是

[...]

你需要做的是甚至不要使用 ShibePhoto 类,而是在代码中使用 List&lt;String&gt;,以取代你之前使用 ShibePhoto 的地方。

英文:

You are basically saying that the response is of the form

{&quot;photos&quot;:[...]}

Instead of just

[...]

What you need to do is not even have the class ShibePhoto and use List&lt;String&gt; directly wherever you had ShibePhoto in your code

huangapple
  • 本文由 发表于 2023年7月31日 21:47:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定