GSON:如何将字段移动到父对象 [英] GSON: How to move fields to parent object
问题描述
我使用 Google GSON 将我的Java对象转换为JSON。
目前我的结构如下:
Step :{
start_name:Start,
end_name:End,
data:{
duration:{
value :292,
text:4分钟。
,
distance:{
value:1009.0,
text:1 km
},
location :{
lat:59.0000,
lng:9.0000,
alt:0.0
}
}
}
目前持续时间
对象位于数据
对象。我想跳过 Data
对象并将持续时间
对象移动到 Step
object,像这样:
Step:{
start_name:Start ,
end_name:End,
duration:{
value:292,
text:4分钟。
,
distance:{
value:1009.0,
text:1 km
},
location :{
lat:59.0000,
lng:9.0000,
alt:0.0
}
}
我如何使用GSON来做这件事?
编辑:我试过使用TypeAdapter修改Step.class,但是在写入方法中,我无法将持续时间对象添加到JsonWriter中。
持续时间$ c $一起工作c>等,而不是 Data
。 //注册您的自定义序列化程序:
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Step.class,new StepSerializer());
Gson gson = builder.create();
//现在使用'gson'完成所有工作
自定义代码下面的串行器,我正在写我的头顶。它错过了异常处理,并且可能无法编译,并且反复减慢了诸如 Gson
的创建实例的速度。但它代表了你想做的事情:
class StepSerializer实现了JsonSerializer< em>工序>
{
公共JsonElement序列化(步骤SRC,
输入typeOfSrc,
JsonSerializationContext上下文)
{
GSON GSON =新GSON();
/ *只要序列化步骤,
就可以正确地序列化包含的数据。 * /
JsonObject step = new JsonObject();
step.add( start_name,gson.toJsonTree(src.start_name);
step.add( end_name,gson.toJsonTree(src.end_name);
/ *注意我是如何深入到'数据'中的2个层次,但是在'step'本身中增加了1个深度的
JSON元素。* /
step.add(duration,gson.toJsonTree (src.data.duration);
step.add( 距离,gson.toJsonTree(src.data.distance);
step.add( 位置,gson.toJsonTree(src.data .location);
return step;
}
}
I'm using Google GSON to transform my Java object to JSON.
Currently I'm having the following structure:
"Step": {
"start_name": "Start",
"end_name": "End",
"data": {
"duration": {
"value": 292,
"text": "4 min."
},
"distance": {
"value": 1009.0,
"text": "1 km"
},
"location": {
"lat": 59.0000,
"lng": 9.0000,
"alt": 0.0
}
}
}
Currently a Duration
object is inside a Data
object. I would like to skip the Data
object and move the Duration
object to the Step
object, like this:
"Step": {
"start_name": "Start",
"end_name": "End",
"duration": {
"value": 292,
"text": "4 min."
},
"distance": {
"value": 1009.0,
"text": "1 km"
},
"location": {
"lat": 59.0000,
"lng": 9.0000,
"alt": 0.0
}
}
How can I do this using GSON?
EDIT: I've tried to use a TypeAdapter to modify the Step.class, but in the write-method I'm not able to add my duration object to the JsonWriter.
解决方案 You can probably do this by writing, and then registering a custom serializer for Step
, and making sure inside it you work with Duration
etc, instead of Data
.
// registering your custom serializer:
GsonBuilder builder = new GsonBuilder ();
builder.registerTypeAdapter (Step.class, new StepSerializer ());
Gson gson = builder.create ();
// now use 'gson' to do all the work
The code for the custom serializer below, I'm writing off the top of my head. It misses exception handling, and might not compile, and does slow things like create instances of Gson
repeatedly. But it represents the kind of thing you'll want to do:
class StepSerializer implements JsonSerializer<Step>
{
public JsonElement serialize (Step src,
Type typeOfSrc,
JsonSerializationContext context)
{
Gson gson = new Gson ();
/* Whenever Step is serialized,
serialize the contained Data correctly. */
JsonObject step = new JsonObject ();
step.add ("start_name", gson.toJsonTree (src.start_name);
step.add ("end_name", gson.toJsonTree (src.end_name);
/* Notice how I'm digging 2 levels deep into 'data.' but adding
JSON elements 1 level deep into 'step' itself. */
step.add ("duration", gson.toJsonTree (src.data.duration);
step.add ("distance", gson.toJsonTree (src.data.distance);
step.add ("location", gson.toJsonTree (src.data.location);
return step;
}
}
这篇关于GSON:如何将字段移动到父对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!