Deserializing json array using gson

前端 未结 4 1413
深忆病人
深忆病人 2020-12-16 18:34

Continuing from this question.

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

\"geometry\": { \"type\": \"Polygon\"         


        
4条回答
  •  萌比男神i
    2020-12-16 19:27

    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() {}.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>(){}.getType() 
    

    Hope this helps.

提交回复
热议问题