使用Moshi和Room解码复杂json的最佳方法是什么?

时间:2018-08-13 03:12:52

标签: json kotlin decode android-room moshi

我目前的响应为JSON

{"status":true,"data":[{"job_assigned_id":"2","job_id":"5","user_id":"10","job_progress_id":"1","created":"Aug 10 2018 05:19:33 PM","sync_status":"0","description":"Sample Job Sheet for Any Breed and Type of Tree","job_stat_id":"1","job_stat_name":"Pending","trees":[{"tree_id":"1","tree_name":"Durian Tree 1","tree_status_id":"1","breed_id":"1","date_planted":"Jul 18 2018","harvest_date":"Jul 20 2018","tree_group":"A1","tree_row":"1","tree_status_name":"Seedling","breed_name":"Durian Philippines"},{"tree_id":"3","tree_name":"Durian Tree 3","tree_status_id":"1","breed_id":"1","date_planted":"Jul 19 2018","tree_group":"A1","tree_row":"1","tree_status_name":"Seedling","breed_name":"Durian Philippines"}]},{"job_assigned_id":"3","job_id":"5","user_id":"10","job_progress_id":"2","created":"Aug 11 2018 03:26:04 PM","sync_status":"0","description":"Sample Job Sheet for Any Breed and Type of Tree","job_stat_id":"1","job_stat_name":"Pending","trees":[{"tree_id":"1","tree_name":"Durian Tree 1","tree_status_id":"1","breed_id":"1","date_planted":"Jul 18 2018","harvest_date":"Jul 20 2018","tree_group":"A1","tree_row":"1","tree_status_name":"Seedling","breed_name":"Durian Philippines"},{"tree_id":"3","tree_name":"Durian Tree 3","tree_status_id":"1","breed_id":"1","date_planted":"Jul 19 2018","tree_group":"A1","tree_row":"1","tree_status_name":"Seedling","breed_name":"Durian Philippines"},{"tree_id":"7","tree_name":"Durian 12","tree_status_id":"2","breed_id":"1","date_planted":"Jul 26 2018","tree_group":"A1","tree_row":"1","tree_status_name":"Small Plant","breed_name":"Durian Philippines"}]}]}

并且我的项目中有这些DTO

@Entity(tableName = "job")
data class Job(

    @PrimaryKey
    @ColumnInfo(name = "job_id")
    @Json(name = "job_id")
    var addressID: Int = 0,

    @ColumnInfo(name = "description")
    @Json(name ="description")
    var description: String,

    @ColumnInfo(name = "created")
    @Json(name = "created")
    var created: String,

    @ColumnInfo(name = "deleted")
    @Json(name = "deleted")
    var deleted: String,

    @ColumnInfo(name = "sync_status")
    @Json(name ="sync_status")
    var syncStatus: String

): Serializable

和另一个JobAssigned

@Entity(tableName = "job_assigned",
    foreignKeys = arrayOf(ForeignKey(entity = Job::class,
            parentColumns = arrayOf("job_id"),
            childColumns = arrayOf("job_id"),
            onDelete = ForeignKey.CASCADE)) )

data class JobAssigned(
    @PrimaryKey
    @ColumnInfo(name = "job_assigned_id")
    @Json(name = "job_assigned_id")
    var jobAssignedID: Int = 0,

    @ColumnInfo(name = "job_id")
    @Json(name = "job_id")
    var jobID: Int = 0,

    @ColumnInfo(name = "user_id")
    @Json(name = "user_id")
    var userID: Int = 0,

    @ColumnInfo(name = "tree_group_id")
    @Json(name = "tree_group_id")
    var treeGroupID: Int = 0,

    @ColumnInfo(name = "job_progress_id")
    @Json(name = "job_progress_id")
    var jobProgressID: Int = 0,

    @ColumnInfo(name = "created")
    @Json(name = "created")
    var created: String?,

    @ColumnInfo(name = "deleted")
    @Json(name = "deleted")
    var deleted: String?,

    @ColumnInfo(name = "sync_status")
    @Json(name = "sync_status")
    var syncStatus: Int = 0,

    @Ignore
    var ignored: String? = null) : Serializable

当前,我正在使用此代码解码响应

val moshi = Moshi.Builder()
            .add(KotlinJsonAdapterFactory())
            .build()
val jobAssignAdapter = moshi.adapter(JobAssigned::class.java)

for(i in 0 until response.dataArray.length()) {
        val jobAssign = 
jobAssignAdapter.fromJson(response.dataArray[i].toString())
        mJobAssign.add(jobAssign)
    }

解码此响应的最佳方法是什么?我是否需要调整DTO以便能够正确捕获?我正在尝试对所有回复做出简单的dto。

0 个答案:

没有答案