使用gson反序列化json数组

继续这个问题 。

我无法反序列化以下json数组(抱歉大小):

"geometry": { "type": "Polygon", "coordinates": [ [ [ 771230.894373, 4422896.962001 ], [ 804804.852796, 4451159.130080 ], [ 876828.563339, 4417873.954498 ], [ 959794.979827, 4430944.287708 ], [ 910992.515063, 4372980.866944 ], [ 932488.308736, 4357684.778349 ], [ 918573.372386, 4115663.286966 ], [ 834059.614976, 4013708.358795 ], [ 929360.231044, 3833522.241529 ], [ 1008029.715188, 3776446.653183 ], [ 1061663.445852, 3533717.758754 ], [ 1035703.740599, 3519308.069656 ], [ 1095348.723766, 3396028.487184 ], [ 1108462.159782, 3230455.268230 ], [ 1083571.121640, 3163122.508021 ], [ 1103953.720405, 3082716.041755 ], [ 1045722.494771, 3020215.642212 ], [ 1117367.719045, 2915275.458735 ], [ 1141268.013718, 2827405.304519 ], [ 1286729.192338, 2790314.754276 ], [ 1334329.406601, 2695307.513404 ], [ 829417.592210, 2374337.277646 ], [ 647042.870444, 2207530.090128 ], [ 370914.873531, 2152159.656850 ], [ 346669.488436, 2173360.227237 ], [ 359905.375891, 2251757.174668 ], [ 199905.871774, 2309591.361246 ], [ 129963.835709, 2361036.252651 ], [ 130208.738589, 2404106.913263 ], [ -964785.432600, 3159802.671416 ], [ -964829.960396, 3338713.127631 ], [ -851005.781060, 3424742.002477 ], [ - 616522.405653, 3491025.523892 ], [ -547749.224241, 3569019.334331 ], [ -403724.067052, 3628920.873754 ], [ -423973.082428, 3724062.779415 ], [ -333893.350478, 3741450.793542 ], [ -317696.364567, 3774909.265404 ], [ -131414.328674, 3777826.527844 ], [ - 112467.751341, 3830221.719769 ], [ -185682.580436, 3930014.456814 ], [ -194499.084106, 4129581.855629 ], [ -245950.952751, 4175549.526399 ], [ -42303.076294, 4287174.981681 ], [ -11222.674464, 4271148.905617 ], [ 131633.628071, 4371332.547494 ], [ 433220.392528, 4427574.250017 ], [ 593119.709103, 4389089.571176 ], [ 719645.442339, 4451856.882422 ], [ 771230.894373, 4422896.962001 ] ] ] } 

如果我将它粘贴到json-viewer中,我会得到这样的结构:

 [geometry] ... [coordinates] => Array ( [0] => Array ( [0] => Array ( [0] => 771230.894373 [1] => 4422896.962001 ) [1] => Array ( [0] => 804804.852796 [1] => 4451159.13008 ) ... [n] => Array [n] => Array 

现在,包含具有坐标的数组的数组具有可变大小。 所以我认为在java中,整个对象应该是一个数组,包含一个数组集合,每个数组包含一个Collection 。 像Collection[][].

但是gson不接受这个。 我收到以下错误消息:

 Exception in thread "main" com.google.gson.JsonParseException: Expecting object but found array: 2.963610 

这看起来很奇怪,因为2.963610对我来说看起来不像一个arrays。 但它可能让我迷茫到我失去的地步,或多或少……

我想我知道你的问题来自哪里,阅读Gson API:

如果序列化/反序列化的对象是ParameterizedType(即包含至少一个类型参数并且可能是数组),则必须使用toJson(Object,Type)或fromJson(String,Type)方法。 以下是序列化和解析ParameterizedType的示例:

 Type listType = new TypeToken() {}.getType(); List target = new LinkedList(); target.add("blah"); Gson gson = new Gson(); String json = gson.toJson(target, listType); List target2 = gson.fromJson(json, listType); 

知道

 Type typeOfCollectionOfFoo = new TypeToken>(){}.getType() 

希望这可以帮助。

JSON中的coordinates是三维矩阵。 使用Collection[][]你走得太远了。 Collection本身已经是一个维度,所以你基本上已经声明了一个四维矩阵。

有了错误信息,Gson基本上告诉你它正在期待第四维的对象,但它却遇到了一个双重问题。

以下代表有效的三维矩阵,应由Gson完美处理:

  • private double[][][] coordinates; (推荐的)
  • private Collection[] coordinates;
  • private Collection coordinates;
  • private Collection> coordinates;
  • private Collection>> coordinates;

也就是说,在这个特殊情况下,我更喜欢List以上的Collection 。 使用List您可以保证它已经填充了插入顺序,您将能够通过索引获取元素。

根据Gson用户指南:

序列化和反序列化generics类型
当你调用toJson(obj)时,Gson调用obj.getClass()来获取有关要序列化的字段的信息。 类似地,您通常可以在fromJson(json, MyClass.class)方法中传递MyClass.class对象。 只要对象是非generics类型,这样就可以正常工作。 但是,如果对象是generics类型,则由于Java类型擦除而丢失generics类型信息。 这是一个说明这一点的例子:

 List myStrings = new List(); gson.toJson(myStrings); // Will cause a runtime exception gson.fromJson(json, myStrings.getClass()); 

上面的调用导致运行时exception,因为Gson调用myStrings.getClass()来获取其类信息,但此方法返回一个原始类List.class 。 这意味着Gson无法知道这是一个字符串列表,而不是普通对象。

您可以通过为generics类型指定正确的参数化类型来解决此问题。 您可以使用TypeToken类来完成此操作。

 Type listType = new TypeToken>() {}.getType(); gson.toJson(myStrings, listType); gson.fromJson(json, listType); 

用于获取listType的习惯用法实际上定义了一个匿名本地内部类,其中包含一个返回完全参数化类型的方法getType()

希望这可以帮助。

我想我们需要更多细节,比如你为反序列化所写的内容。