拼合嵌套的对象与GSON目标对象 [英] Flatten Nested Object into target object with GSON
问题描述
亲爱Stackoverflowers,
Dearest Stackoverflowers,
我不知道是否有人知道如何解决它的最好方法;
我说的就是它返回一个JSON对象像这样的API:
I was wondering if anyone knows how to solve this the best way; I'm talking to an api which returns a json object like this:
{
"field1": "value1",
"field2": "value2",
"details": {
"nested1": 1,
"nested2": 1
}
在java的我有一个对象(实体)其中,例如,将具有所有这些领域,但与细节松散字段,因此:
字段1,字段2,nested1,nested2。
In java I have an object (entity) which for example, would have all these fields, but with the details as loose fields, so: field1, field2, nested1, nested2.
这是因为它是一个Android项目,我不能只是去拯救与信息的一类到我的实体,因为我不能不ormlite。
This because It's an android project and I can't just go saving a class with info into my entity since I'm bound to ormlite.
有没有办法转换使用GSON平田到我的对象吗?请注意,我用一个泛型类这些权直接从API现在转换。我要存储这些字段(其中包含的信息作为int)。在相同的实体。
Is there any way to convert the fields flat into my object using GSON? note that I'm using a generic class to convert these right now straight from the API. And I want to store these fields (which contain information as an int). In the same entity.
推荐答案
我结束了检查,如果JSON数组包含包含细节的获取对象并将其合并到根元素。
I ended up checking if the json array contained elements containing details fetching that object and merging it into the root.
不是最好的解决方案,但因为我有10个对象它不是一个巨大的冲击的最大工作和它的作品。
Not the best solution but since I'm working with a max of 10 objects it's not a huge impact and it works.
这篇关于拼合嵌套的对象与GSON目标对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!