如何为具有可变数量变量的JSON对象创建DTO结构?

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

How can I create a DTO structure for JSON objects with a variable number of variables?

问题

我在将API数据转换为Kotlin中的Java对象时遇到了问题。例如,如果API中有两个信息片段,我应该创建一个数据类如下:

data class Persons(
    val person1: Person,
    val person2: Person
)

但如果有五个信息片段,我应该创建一个数据类如下:

data class Persons(
    val person1: Person,
    val person2: Person,
    val person3: Person,
    val person4: Person,
    val person5: Person
)

我不确定如何处理这种情况,因为API中的信息片段数量可能会在每个请求中发生变化。有时只有一个信息片段,而其他时候可能一次出现20个信息片段。我应该怎么处理?

另外,这是我的Retrofit构建器:

@Provides
@Singleton
fun provideRetrofit(): ExampleApi{
    return Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
        .create(ExampleApi::class.java)
}

我现在该怎么办?我认为我缺少一些东西,需要一些帮助。另外,我提供了一个可能有用的示例JSON响应,因为我的API调用是成功的,但我在DTO方面遇到了问题。以下是示例JSON:

{
    "person1": {
        "name": {
            "name": "hello",
            "surname": "hello"
        },
        "gender": {
            "topRank": 10,
            "age": 10
        }
    },
    "person2": {
        "name": {
            "name": "hello",
            "surname": "hello"
        },
        "gender": {
            "topRank": 10,
            "age": 10
        }
    }
}
英文:

I'm having trouble converting data from an API to a Java object in Kotlin. For example, if there are two pieces of information in the API, I should make a data class like this:

data class Persons(
    val person1: Person,
    val person2: Person
)

But if there are five pieces of information, I should make a data class like this:

data class Persons(
    val person1: Person,
    val person2: Person,
    val person3: Person,
    val person4: Person,
    val person5: Person,
)

I'm not sure how to handle this situation, because the number of pieces of information that can come from the API can change with each request. Sometimes only one piece of information comes, while other times 20 pieces can come at once. What should I do to handle this?

by the way, this is my cute retrofit builder;

    @Provides
    @Singleton
    fun provideRetrofit(): ExampleApi{
        return Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(ExampleApi::class.java)
    }

What should I do now? I think I'm missing something and I need some help. Additionally, I'm providing an example JSON response that might be useful, as my API call is successful but I'm having trouble with the DTO. Here's the example JSON:

{
    "person1": {
    "name": {
        "name": "hello",
        "surname": "hello",
    },
    "gender": {
        "topRank": 10,
        "age": 10,
    }
},
    "person2": {
    "name": {
        "name": "hello",
        "surname": "hello",
    },
    "gender": {
        "topRank": 10,
        "age": 10,
    }
}
}

答案1

得分: 1

你尝试使用 Map 吗?

class Example(
    val Map<String,Person> response:
)

并在响应中包装一个键

{
    "result": {
        "person1": {
            "name": {
                "name": "hello",
                "surname": "hello"
            },
            "gender": {
                "topRank": 10,
                "age": 10
            }
        },
        "person2": {
            "name": {
                "name": "hello",
                "surname": "hello"
            },
            "gender": {
                "topRank": 10,
                "age": 10
            }
        }
    }
}

关键是使用 Example 类包装响应并进行转换。

英文:

Have you tried using Map ?

class Example(
    val Map<String,Person> response:
)

and wrap the response in a key

 {
    "result": {
        "person1": {
        "name": {
            "name": "hello",
            "surname": "hello",
        },
        "gender": {
            "topRank": 10,
            "age": 10,
        }
    },
        "person2": {
        "name": {
            "name": "hello",
            "surname": "hello",
        },
        "gender": {
            "topRank": 10,
            "age": 10,
        }
    }
    }

}

the key is to wrap the response with a new object key "response" and cast it using Example class

答案2

得分: 1

这个问题很难解决,我建议你与后端沟通,让他将其修改为以下格式:

{
    "result": [
        {
            "name": {
                "name": "hello",
                "surname": "hello"
            },
            "gender": {
                "topRank": 10,
                "age": 10
            }
        },
        {
            "name": {
                "name": "hello",
                "surname": "hello"
            },
            "gender": {
                "topRank": 10,
                "age": 10
            }
        }
    ]
}

然后你可以使用 List 来接收它:

List<Person>
英文:

This problem is difficult to solve, I suggest you talk to the backend and get him to modify to the following format:

{
&quot;result&quot;: [
    {
        &quot;name&quot;: {
            &quot;name&quot;: &quot;hello&quot;,
            &quot;surname&quot;: &quot;hello&quot;
        },
        &quot;gender&quot;: {
            &quot;topRank&quot;: 10,
            &quot;age&quot;: 10
        }
    },
    {
        &quot;name&quot;: {
            &quot;name&quot;: &quot;hello&quot;,
            &quot;surname&quot;: &quot;hello&quot;
        },
        &quot;gender&quot;: {
            &quot;topRank&quot;: 10,
            &quot;age&quot;: 10
        }
    }
 ]
}

Then you can use List to receive it:

List&lt;Person&gt;

huangapple
  • 本文由 发表于 2023年2月27日 14:12:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577260.html
匿名

发表评论

匿名网友

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

确定