使用gson反序列化json数组 [英] Deserializing json array using gson

查看:126
本文介绍了使用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
],[ [
1334329.406601,2695307.513404],[829417.592210,2374337.277646],[647042.870444,
2207530.090128],[370914.873531,2152159.656850],[346669.488436,2173360.227237],$ [
$ b 1334329.406601,2695307.513404] b $ b [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 $ b $ [ -
112467.751341,3830221.719769],[-185682.580436,3930014.456814],[ - 194499.084106,
4129581.855629],[ - 245950.952751],[-317696.364567,3774909.265404],[ - 131414.328674,3777826.527844] 4175549.526399],[-42303.076294,4287174.981681
],[-11222 [
433220.392528,4427574.250017],[593119.709103,4389089.571176],[719645.442339,
4451856.882422],[771230.894373,4422896.962001]]]}

如果我将它粘贴到json查看器中,我会得到以下结构:

  [geometry] 
...
[coordinates] =>数组

[0] =>数组

[0] =>数组

[0] => 771230.894373
[1] => 4422896.962001

[1] =>数组

[0] => 804804.852796
[1] = > 4451159.13008

...
[n] =>数组
[n] =>数组
pre>

现在,包含坐标数组的数组具有可变大小,所以我认为在java中,整个对象应该是一个数组,包含数组,每个数组都包含一个集合< double []> 类似于集合< double []> [] []。



但是gson不接受这个,我得到以下错误信息:

$ $ $ p> 线程main中的异常com.google.gson.JsonParseException:期望对象,但
找到数组:$ 2.963610

这似乎很奇怪,因为 2.963610 看起来不像我的数组。但它可能会让我困惑到我迷失的地步,或多或少......

我想我知道如果您的问题来自读取Gson API:


如果
序列化/反序列化的对象是
ParameterizedType(即包含
至少一个类型参数,可能是
数组),那么您必须使用
toJson(Object,Type)或
fromJson(String,Type ) 方法。这里是
序列化的一个例子,
展示了一个ParameterizedType:



  Type listType = new TypeToken< LinkedList>(){} .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< Collection< Foo>>(){}。getType()

希望这会有所帮助。


Continuing from this question.

I'm having trouble deserializing the following json array (Sorry for the size):

"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 ] ] ] }

If I paste it into a json-viewer, I get this structure:

[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

Now, the array containing the arrays with the coordinates has a variable size. So I figured that in java, this whole object whould be an array, containing a collection of arrays, with each array containing a Collection<double[]>. Something like Collection<double[]>[][].

But gson does not accept this. I get the following error message:

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

Which seems weird, as 2.963610 doesnt look like an array to me. But it might have confused me to the point where I'm lost, more or less...

解决方案

I think I know where your problem comes from, reading the Gson API :

If the object that your are serializing/deserializing is a ParameterizedType (i.e. contains at least one type parameter and may be an array) then you must use the toJson(Object, Type) or fromJson(String, Type) method. Here is an example for serializing and deserialing a ParameterizedType:

Type listType = new TypeToken<LinkedList>() {}.getType();
List target = new LinkedList();
target.add("blah");

Gson gson = new Gson();
String json = gson.toJson(target, listType);
List target2 = gson.fromJson(json, listType);

Knowing that

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

Hope this helps.

这篇关于使用gson反序列化json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆