Gson反序列化Map& K,V& gt;由WCF Rest服务序列化 [英] Gson deserialize Map<K,V> serialized by WCF Rest service
问题描述
我有WCF休息服务。我想在Android设备上使用它。为此,我选择了gson库。在我想从我的服务包含对象的地图返回之前,一切似乎都工作正常。 Gson将其序列化(并且我假设还会尝试反序列化),例如:
I have a WCF Rest Service. I would like to use it from Android device. To do this I have chosen gson library. Everything seemed to work fine until I wanted to return from my service Object Containing Map. Gson serializes it (and as I am assuming also tries to deserialize from it) as something like:
{"org.Mob.ComplexObject@3dac2f9c":"TAX1","org.Mob.ComplexObject@7369ca65":"TAX2"}
但是我的服务发送的json看起来像:
But the json sent by my service looks like:
{"Key":
{"DefaultValue":"True",
"Description":null,
"DisplayName":"Custom Boolean",
"FieldType":0,
"Id":6,
"IsReadOnly":false,
"IsRequired":false,
"MaxLength":null,
"Name":"BoolVal",
"ParamType":0},
"Value":"True"},
{"Key":
{"DefaultValue":"",
"Description":null,
"DisplayName":"Custom Text",
"FieldType":4,
"Id":7,
"IsReadOnly":false,
"IsRequired":true,
"MaxLength":16,
"Name":"TextVal",
"ParamType":0},
"Value":"sda"}
}
推荐答案
另一种方法是添加您自己的Map / Dictionary实现,该实现扩展了ArrayList,实际上是一个KVPair列表,其中KVPair如下:
The other way to do it could be adding you own implementation of Map / Dictionary which extends ArrayList, and in fact is a List of KVPair, where KVPair looks like:
class KVPair<K,V>{
K Key;
V Value;
}
我不知道它是否优雅,但在我的情况下有效。
I don't know if it's elegant, but it worked in my case.
这篇关于Gson反序列化Map& K,V& gt;由WCF Rest服务序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!