Retrofit android预期为开始_ARRAY,但在第1行第2列路径$处为BEGIN_OBJECT

ioekq8ef  于 2023-04-04  发布在  Android
关注(0)|答案(1)|浏览(111)

我是新的改造,我有一个奇怪的问题。我得到以下错误--〉
应为开始_ARRAY,但在第1行第2列路径$处为BEGIN_OBJECT
然而,当我用curl命令访问端点时,我得到了一个json数组。
编辑
抱歉所有的编辑。我可能过早地提出了这个问题。我正在考虑删除不必要的代码片段,如果它们分散注意力的话。我把编辑放在顶部,这样你就不必向下滚动来查看最新的问题。我想我可能在最后一个问题上遇到了一些真实的的麻烦。
我发现使用不同的模型结构有助于json转换。

data class Jobs(
    val job: List<JobPW>,
    val status : String
)

到这个

data class JobApiModel(
    val jobs: List<Job>,
    val status: String
)

并使用匹配的元素创建了一个新的Job类

data class Job(
    @SerializedName("city")
    @field:Json(name = "city")
    val city: String,
    @SerializedName("job_id")
    @field:Json(name = "job_id")
    val job_id: Int,
    @SerializedName("owner_id_map")
    @field:Json(name = "owner_id_map")
    val owner_id_map: Int,
    @SerializedName("status")
    @field:Json(name = "status")
    val status: Int,
    @SerializedName("title")
    val title: String,
    val workers: List<Worker>,
    val posters: List<PosterWithWorker>
)

这是与JobPW相同的对象,但它被称为Job,这是属性的名称。您可以使用@SerializedName或类似的类名吗?JobPW是一个带有Job对象的Room关系,我不能将两个类都称为Job。处理此问题的最佳方法是什么?
所以两者

workers : List<Workers>

List<PosterWithWorker>

反序列化正确。其他字段都是null和空的。有什么我做错了或留下什么要做的吗?
编辑
我越来越接近了,似乎在数组周围添加了外部jobs对象之后,调用不再因异常而失败。相反,我从Retrofit response = Jobs(job = null)获得了一个null数组(jobs = null)。
我的房间刀对象在下面靠近底部的职位
由于某些原因,使用Retrofit时JSON响应看起来不同。
我应该提到这是来自一个Firebase实时数据库。当我使用curl时,我只看到数组,没有 Package 对象。

{
  "jobs": [
    {
      "job": {
        "city": "Toronto",
        "job_id": 1,
        "owner_id_map": 1,
        "status": 0,
        "title": "500 Posters in Toronto"
      },
      "posters": [
        {
          "poster": {
            "job_id_map": 2,
            "latitude": 43.64526533333377,
            "locationDescription": "Union Station",
            "longitude": -79.38060468539248,
            "poster_id": 1,
            "qr_code": "code2",
            "worker_id": 1
          },
          "worker": [
            {
              "workerName": "Noris",
              "worker_id": 1
            }
          ]
        },
        {
          "poster": {
            "job_id_map": 2,
            "latitude": 43.64295669900133,
            "locationDescription": "Queen and Spadina",
            "longitude": -79.39368099382459,
            "poster_id": 2,
            "qr_code": "code3",
            "worker_id": 2
          },
          "worker": [
            {
              "workerName": "Jack",
              "worker_id": 2
            }
          ]
        },
        {
          "poster": {
            "job_id_map": 3,
            "latitude": 43.76892443268843,
            "locationDescription": "North York Civic Center",
            "longitude": -79.41292484238735,
            "poster_id": 3,
            "qr_code": "code4",
            "worker_id": 3
          },
          "worker": [
            {
              "workerName": "Nicky",
              "worker_id": 3
            }
          ]
        }
      ],
      "workers": [
        {
          "workerName": "Noris",
          "worker_id": 1
        },
        {
          "workerName": "Jack",
          "worker_id": 2
        },
        {
          "workerName": "Nicky",
          "worker_id": 3
        },
        {
          "workerName": "Davis",
          "worker_id": 4
        },
        {
          "workerName": "Argyle",
          "worker_id": 5
        },
        {
          "workerName": "James",
          "worker_id": 6
        }
      ]
    },
    {
      "job": {
        "city": "Montreal",
        "job_id": 2,
        "owner_id_map": 1,
        "status": 1,
        "title": "20 Posters in Montreal"
      },
      "posters": [],
      "workers": [
        {
          "workerName": "Noris",
          "worker_id": 1
        },
        {
          "workerName": "Jack",
          "worker_id": 2
        },
        {
          "workerName": "Nicky",
          "worker_id": 3
        },
        {
          "workerName": "Davis",
          "worker_id": 4
        },
        {
          "workerName": "Argyle",
          "worker_id": 5
        },
        {
          "workerName": "James",
          "worker_id": 6
        }
      ]
    },
    {
      "job": {
        "city": "Ottawa",
        "job_id": 3,
        "owner_id_map": 2,
        "status": 2,
        "title": "350 Posters in Ottawa"
      },
      "posters": [
        {
          "poster": {
            "job_id_map": 4,
            "latitude": 43.63904362027761,
            "locationDescription": "Harbourfront",
            "longitude": -79.38193214610098,
            "poster_id": 4,
            "qr_code": "code5",
            "worker_id": 0
          },
          "worker": []
        },
        {
          "poster": {
            "job_id_map": 5,
            "latitude": 43.68347235901327,
            "locationDescription": "Bathurst and St Clair",
            "longitude": -79.41851002097324,
            "poster_id": 5,
            "qr_code": "code6",
            "worker_id": 0
          },
          "worker": []
        },
        {
          "poster": {
            "job_id_map": 2,
            "latitude": 43.6958481647278,
            "locationDescription": "Dufferin and Eglinton",
            "longitude": -79.45035794609939,
            "poster_id": 6,
            "qr_code": "code7",
            "worker_id": 0
          },
          "worker": []
        }
      ],
      "workers": [
        {
          "workerName": "Noris",
          "worker_id": 1
        },
        {
          "workerName": "Jack",
          "worker_id": 2
        },
        {
          "workerName": "Nicky",
          "worker_id": 3
        },
        {
          "workerName": "Davis",
          "worker_id": 4
        },
        {
          "workerName": "Argyle",
          "worker_id": 5
        },
        {
          "workerName": "James",
          "worker_id": 6
        }
      ]
    },
    {
      "job": {
        "city": "Calgary",
        "job_id": 4,
        "owner_id_map": 2,
        "status": 0,
        "title": "10 Posters in Calgary"
      },
      "posters": [
        {
          "poster": {
            "job_id_map": 2,
            "latitude": 43.64526533333377,
            "locationDescription": "Union Station",
            "longitude": -79.38060468539248,
            "poster_id": 1,
            "qr_code": "code2",
            "worker_id": 1
          },
          "worker": [
            {
              "workerName": "Noris",
              "worker_id": 1
            }
          ]
        },
        {
          "poster": {
            "job_id_map": 2,
            "latitude": 43.64295669900133,
            "locationDescription": "Queen and Spadina",
            "longitude": -79.39368099382459,
            "poster_id": 2,
            "qr_code": "code3",
            "worker_id": 2
          },
          "worker": [
            {
              "workerName": "Jack",
              "worker_id": 2
            }
          ]
        },
        {
          "poster": {
            "job_id_map": 3,
            "latitude": 43.76892443268843,
            "locationDescription": "North York Civic Center",
            "longitude": -79.41292484238735,
            "poster_id": 3,
            "qr_code": "code4",
            "worker_id": 3
          },
          "worker": [
            {
              "workerName": "Nicky",
              "worker_id": 3
            }
          ]
        }
      ],
      "workers": [
        {
          "workerName": "Noris",
          "worker_id": 1
        },
        {
          "workerName": "Jack",
          "worker_id": 2
        },
        {
          "workerName": "Nicky",
          "worker_id": 3
        },
        {
          "workerName": "Davis",
          "worker_id": 4
        },
        {
          "workerName": "Argyle",
          "worker_id": 5
        },
        {
          "workerName": "James",
          "worker_id": 6
        }
      ]
    }
  ],
  "status": "success"
}

有人知道为什么会这样吗?为什么旋度React会不同?
下面是Firebase中使用curl的json。

[
  {
    "job": {
      "city": "Toronto",
      "job_id": 1,
      "owner_id_map": 1,
      "status": 0,
      "title": "500 Posters in Toronto"
    },
    "posters": [
      {
        "poster": {
          "job_id_map": 2,
          "latitude": 43.64526533333377,
          "locationDescription": "Union Station",
          "longitude": -79.38060468539248,
          "poster_id": 1,
          "qr_code": "code2",
          "worker_id": 1
        },
        "worker": [
          {
            "workerName": "Noris",
            "worker_id": 1
          }
        ]
      },
      {
        "poster": {
          "job_id_map": 2,
          "latitude": 43.64295669900133,
          "locationDescription": "Queen and Spadina",
          "longitude": -79.39368099382459,
          "poster_id": 2,
          "qr_code": "code3",
          "worker_id": 2
        },
        "worker": [
          {
            "workerName": "Jack",
            "worker_id": 2
          }
        ]
      },
      {
        "poster": {
          "job_id_map": 3,
          "latitude": 43.76892443268843,
          "locationDescription": "North York Civic Center",
          "longitude": -79.41292484238735,
          "poster_id": 3,
          "qr_code": "code4",
          "worker_id": 3
        },
        "worker": [
          {
            "workerName": "Nicky",
            "worker_id": 3
          }
        ]
      }
    ],
    "workers": [
      {
        "workerName": "Noris",
        "worker_id": 1
      },
      {
        "workerName": "Jack",
        "worker_id": 2
      },
      {
        "workerName": "Nicky",
        "worker_id": 3
      },
      {
        "workerName": "Davis",
        "worker_id": 4
      },
      {
        "workerName": "Argyle",
        "worker_id": 5
      },
      {
        "workerName": "James",
        "worker_id": 6
      }
    ]
  },
  {
    "job": {
      "city": "Montreal",
      "job_id": 2,
      "owner_id_map": 1,
      "status": 1,
      "title": "20 Posters in Montreal"
    },
    "workers": [
      {
        "workerName": "Noris",
        "worker_id": 1
      },
      {
        "workerName": "Jack",
        "worker_id": 2
      },
      {
        "workerName": "Nicky",
        "worker_id": 3
      },
      {
        "workerName": "Davis",
        "worker_id": 4
      },
      {
        "workerName": "Argyle",
        "worker_id": 5
      },
      {
        "workerName": "James",
        "worker_id": 6
      }
    ]
  },
  {
    "job": {
      "city": "Ottawa",
      "job_id": 3,
      "owner_id_map": 2,
      "status": 2,
      "title": "350 Posters in Ottawa"
    },
    "posters": [
      {
        "poster": {
          "job_id_map": 4,
          "latitude": 43.63904362027761,
          "locationDescription": "Harbourfront",
          "longitude": -79.38193214610098,
          "poster_id": 4,
          "qr_code": "code5",
          "worker_id": 0
        }
      },
      {
        "poster": {
          "job_id_map": 5,
          "latitude": 43.68347235901327,
          "locationDescription": "Bathurst and St Clair",
          "longitude": -79.41851002097324,
          "poster_id": 5,
          "qr_code": "code6",
          "worker_id": 0
        }
      },
      {
        "poster": {
          "job_id_map": 2,
          "latitude": 43.6958481647278,
          "locationDescription": "Dufferin and Eglinton",
          "longitude": -79.45035794609939,
          "poster_id": 6,
          "qr_code": "code7",
          "worker_id": 0
        }
      }
    ],
    "workers": [
      {
        "workerName": "Noris",
        "worker_id": 1
      },
      {
        "workerName": "Jack",
        "worker_id": 2
      },
      {
        "workerName": "Nicky",
        "worker_id": 3
      },
      {
        "workerName": "Davis",
        "worker_id": 4
      },
      {
        "workerName": "Argyle",
        "worker_id": 5
      },
      {
        "workerName": "James",
        "worker_id": 6
      }
    ]
  },
  {
    "job": {
      "city": "Calgary",
      "job_id": 4,
      "owner_id_map": 2,
      "status": 0,
      "title": "10 Posters in Calgary"
    },
    "posters": [
      {
        "poster": {
          "job_id_map": 2,
          "latitude": 43.64526533333377,
          "locationDescription": "Union Station",
          "longitude": -79.38060468539248,
          "poster_id": 1,
          "qr_code": "code2",
          "worker_id": 1
        },
        "worker": [
          {
            "workerName": "Noris",
            "worker_id": 1
          }
        ]
      },
      {
        "poster": {
          "job_id_map": 2,
          "latitude": 43.64295669900133,
          "locationDescription": "Queen and Spadina",
          "longitude": -79.39368099382459,
          "poster_id": 2,
          "qr_code": "code3",
          "worker_id": 2
        },
        "worker": [
          {
            "workerName": "Jack",
            "worker_id": 2
          }
        ]
      },
      {
        "poster": {
          "job_id_map": 3,
          "latitude": 43.76892443268843,
          "locationDescription": "North York Civic Center",
          "longitude": -79.41292484238735,
          "poster_id": 3,
          "qr_code": "code4",
          "worker_id": 3
        },
        "worker": [
          {
            "workerName": "Nicky",
            "worker_id": 3
          }
        ]
      }
    ],
    "workers": [
      {
        "workerName": "Noris",
        "worker_id": 1
      },
      {
        "workerName": "Jack",
        "worker_id": 2
      },
      {
        "workerName": "Nicky",
        "worker_id": 3
      },
      {
        "workerName": "Davis",
        "worker_id": 4
      },
      {
        "workerName": "Argyle",
        "worker_id": 5
      },
      {
        "workerName": "James",
        "worker_id": 6
      }
    ]
  }
]

下面是我的Retrofit API调用

@GET("/jobs.json")
   suspend fun getJobsList() : Response<List<JobPW>>

我假设嵌套对象有问题,但我不知道如何调试它。
这是我的模型也是房间对象

data class PosterWithWorker(
    @Embedded val poster: Poster,
    @Relation(
        parentColumn = "worker_id",
        entityColumn = "worker_id",
        associateBy = Junction(
            value = PosterWorkerCrossRef::class,
            parentColumn = "poster_id_map",
            entityColumn = "worker_id_map"
        )
    )
    val worker:  List<Worker>
) : Serializa
package com.davidozersky.posterpal.database.entities

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.PrimaryKey
import java.io.Serializable

@Entity(
    foreignKeys = [

        ForeignKey(
            entity = Owner::class,
            parentColumns = ["owner_id"],
            childColumns = ["owner_id_map"],
            onDelete = ForeignKey.CASCADE,
            onUpdate = ForeignKey.CASCADE
        )
    ]
)
data class Job (

    @PrimaryKey val job_id: Long,
    val status: Int,
    @ColumnInfo(index = true)
    val owner_id_map: Long,
    val title: String,
    val city: String
) : Serializable
package com.davidozersky.posterpal.database.entities

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.io.Serializable

@Entity
class Worker(
    @PrimaryKey
    @ColumnInfo(name = "worker_id")
    val worker_id: Long,
    val workerName: String,
) : Serializable

海报

package com.davidozersky.posterpal.database.entities

import androidx.room.*

@Entity(
    foreignKeys = [
        ForeignKey(
            entity = Job::class,
            parentColumns = ["job_id"],
            childColumns = ["job_id_map"],
            onDelete = ForeignKey.CASCADE,
            onUpdate = ForeignKey.CASCADE
        )
    ]
)
data class Poster (
    @PrimaryKey val poster_id: Long,
    @ColumnInfo(index = true)
    val job_id_map: Long,
    val qr_code: String,
    val worker_id: Long,
    val latitude: Double,
    val longitude: Double,
    val locationDescription: String,
    )

我尝试直接使用OkHttp和HttpLogger,调用的响应是200,并且它记录了相同的json

[{"job":{"city":"Toronto","job_id":1,"owner_id_map":1,"status":0,"title":"500 Posters in Toronto"},"posters":[{"poster":{"job_id_map":2,"latitude":43.64526533333377,"locationDescription":"Union Station","longitude":-79.38060468539248,"poster_id":1,"qr_code":"code2","worker_id":1},"worker":[{"workerName":"Noris","worker_id":1}]},{"poster":{"job_id_map":2,"latitude":43.64295669900133,"locationDescription":"Queen and Spadina","longitude":-79.39368099382459,"poster_id":2,"qr_code":"code3","worker_id":2},"worker":[{"workerName":"Jack","worker_id":2}]},{"poster":{"job_id_map":3,"latitude":43.76892443268843,"locationDescription":"North York Civic Center","longitude":-79.41292484238735,"poster_id":3,"qr_code":"code4","worker_id":3},"worker":[{"workerName":"Nicky","worker_id":3}]}],"workers":[{"workerName":"Noris","worker_id":1},{"workerName":"Jack","worker_id":2},{"workerName":"Nicky","worker_id":3},{"workerName":"Davis","worker_id":4},{"workerName":"Argyle","worker_id":5},{"workerName":"James","worker_id":6}]},{"job":{"city":"Montreal","job_id":2,"owner_id_map":1,"status":1,"title":"20 Posters in Montreal"},"workers":[{"workerName":"Noris","worker_id":1},{"workerName":"Jack","worker_id":2},{"workerName":"Nicky","worker_id":3},{"workerName":"Davis","worker_id":4},{"workerName":"Argyle","worker_id":5},{"workerName":"James","worker_id":6}]},{"job":{"city":"Ottawa","job_id":3,"owner_id_map":2,"status":2,"title":"350 Posters in Ottawa"},"posters":[{"poster":{"job_id_map":4,"latitude":43.63904362027761,"locationDescription":"Harbourfront","longitude":-79.38193214610098,"poster_id":4,"qr_code":"code5","worker_id":0}},{"poster":{"job_id_map":5,"latitude":43.68347235901327,"locationDescription":"Bathurst and St Clair","longitude":-79.41851002097324,"poster_id":5,"qr_code":"code6","worker_id":0}},{"poster":{"job_id_map":2,"latitude":43.6958481647278,"locationDescription":"Dufferin and Eglinton","longitude":-79.45035794609939,"poster_id":6,"qr_code":"code7","worker_id":0}}],"workers":[{"workerName":"Noris","worker_id":1},{"workerName":"Jack","worker_id":2},{"workerName":"Nicky","worker_id":3},{"workerName":"Davis","worker_id":4},{"workerName":"Argyle","worker_id":5},{"workerName":"James","worker_id":6}]},{"job":{"city":"Calgary","job_id":4,"owner_id_map":2,"status":0,"title":"10 Posters in Calgary"},"posters":[{"poster":{"job_id_map":2,"latitude":43.64526533333377,"locationDescription":"Union Station","longitude":-79.38060468539248,"poster_id":1,"qr_code":"code2","worker_id":1},"worker":[{"workerName":"Noris","worker_id":1}]},{"poster":{"job_id_map":2,"latitude":43.64295669900133,"locationDescription":"Queen and Spadina","longitude":-79.39368099382459,"poster_id":2,"qr_code":"code3","worker_id":2},"worker":[{"workerName":"Jack","worker_id":2}]},{"poster":{"job_id_map":3,"latitude":43.76892443268843,"locationDescription":"North York Civic Center","longitude":-79.41292484238735,"poster_id":3,"qr_code":"code4","worker_id":3},"worker":[{"workerName":"Nicky","worker_id":3}]}],"workers":[{"workerName":"Noris","worker_id":1},{"workerName":"Jack","worker_id":2},{"workerName":"Nicky","worker_id":3},{"workerName":"Davis","worker_id":4},{"workerName":"Argyle","worker_id":5},{"workerName":"James","worker_id":6}]}]
2w2cym1i

2w2cym1i1#

@GET("/jobs.json")
suspend fun getJobsList() : Response<List<JobPW>>

为什么你用Response类 Package 你的列表?也许你的Response类和retrofit.Response类不一样。在这种情况下检查你的导入。
其次,检查添加到OkHttp的拦截器,看看是否没有损坏响应。
如果它们对你不起作用,告诉你如何从Retrofit获得JsonObject并显示代码。
编辑
这是一个坏的做法,使用同一个类为您的实体和响应。我写你的响应类。请尝试这一点,让我知道发生了什么。

data class JobsResponseItem(

    @field:SerializedName("posters")
    val posters: List<PostersItem>,

    @field:SerializedName("job")
    val job: Job,

    @field:SerializedName("workers")
    val workers: List<WorkersItem>
){

    data class PostersItem(

        @field:SerializedName("worker")
        val worker: List<WorkerItem>,

        @field:SerializedName("poster")
        val poster: Poster
    ){

        data class WorkerItem(

            @field:SerializedName("workerName")
            val workerName: String,

            @field:SerializedName("worker_id")
            val workerId: Int
        )

        data class Poster(

            @field:SerializedName("latitude")
            val latitude: Double?,

            @field:SerializedName("locationDescription")
            val locationDescription: String,

            @field:SerializedName("job_id_map")
            val jobIdMap: Int,

            @field:SerializedName("qr_code")
            val qrCode: String,

            @field:SerializedName("poster_id")
            val posterId: Int,

            @field:SerializedName("longitude")
            val longitude: Double?,

            @field:SerializedName("worker_id")
            val workerId: Int
        )

    }

    data class Job(

        @field:SerializedName("owner_id_map")
        val ownerIdMap: Int,

        @field:SerializedName("city")
        val city: String,

        @field:SerializedName("job_id")
        val jobId: Int,

        @field:SerializedName("title")
        val title: String,

        @field:SerializedName("status")
        val status: Int
    )

    data class WorkersItem(

        @field:SerializedName("workerName")
        val workerName: String,

        @field:SerializedName("worker_id")
        val workerId: Int
    )
}

并更新restApi如下:

@GET("/jobs.json")
suspend fun getJobsList() : Response<List<JobsResponseItem>>

相关问题