John Smith
John Smith

Reputation: 851

parsing json with Gson java

I have a problem while parsing a json with Gson. here is my code:

Gson gson = new GsonBuilder().create();
String json = gson.toJson(myMap.values());
MyClass clazz = gson.fromJson(json, MyClass.class);
System.out.println(clazz.toString());

But I get the next error(I also tried with new TypeToken, but the errors are the same):

com.google.gson.JsonParseException: The JsonDeserializer MapTypeAdapter failed to deserialized json object /*here is json object*/

Caused by: java.lang.IllegalArgumentException: Map objects need to be parameterized unless you use a custom serializer. Use the com.google.gson.reflect.TypeToken to extract the ParameterizedType.
    at com.google.gson.TypeInfoMap.<init>(TypeInfoMap.java:45)
    at com.google.gson.DefaultTypeAdapters$MapTypeAdapter.deserialize(DefaultTypeAdapters.java:605)
    at com.google.gson.DefaultTypeAdapters$MapTypeAdapter.deserialize(DefaultTypeAdapters.java:573)
    at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:50)

here is obtained json string, it's valid(but may be I should remove the "items" substring?):

["{\n 
    \"items\": 
        [
        \n  {
        \n \"MyClass_type_var1\": {
        \n    \"field1\": \"val1\",
        \n    \"field2\": \"val2\",
        \n    \"field3\": [
                        \n     {
                        \n      \"subfield1\": subval
                        \n     }
                        \n    ]
                        \n   }
                        \n  },
        \n \"MyClass_type_var2\": {
        \n    \"field1\": \"val1\",
        \n    \"field2\": \"val2\",
        \n    \"field3\": [
                        \n     {
                        \n      \"subfield1\": subval
                        \n     }
                        \n    ]
                        \n   }
                        \n  },
        \n \"MyClass_type_var3\": {
        \n    \"field1\": \"val1\",
        \n    \"field2\": \"val2\",
        \n    \"field3\": [
                        \n     {
                        \n      \"subfield1\": subval
                        \n     }
                        \n    ]
                        \n   }
                        \n  },


etc......   may I haven't closed brackets correctly, but they are correct :)                
            }           
        ]               
    "]                  

I would be grateful to any pieces of advice.

public final class MyClass extends GenericJson {

  private String field1;
  private String field2;
  private java.util.List<AClass> field3; // has subfield1
//getters and setters
}

single-line json:

["{\n \"items\": [ \n  { \n \"MyClass\": { \n    \"field1\": \"val1\", \n    \"field2\": \"val2\", \n    \"field2\": [ \n  { \n \"subfield1\": subval   \n } \n    ] \n   } \n  }, \n \"MyClass\": { \n    \"field1\": \"val1\", \n    \"field2\": \"val2\", \n    \"field2\": [ \n     {  \n      \"subfield1\": subval \n     } \n    ] \n   } \n  }, \n \"MyClass\": {\n   \"field1\":\"val1\", \n    \"field2\": \"val2\", \n    \"field2\": [ \n     { \n      \"subfield1\": subval \n     } \n    ] \n   } \n  }, }          ]   "]

Upvotes: 1

Views: 2701

Answers (2)

John Smith
John Smith

Reputation: 851

here i've tried to use jackon, but it didn't helped. see the answer for that question(used my own parser).

Upvotes: 1

Mark Mooibroek
Mark Mooibroek

Reputation: 7706

Try something like this:

MyClass:

import java.util.List;

public class MyClass {

    public List<Item> items;

    public static class Item{

        public List<innerData> MyClass_type_var1;
        public List<innerData> MyClass_type_var2;
        public List<innerData> MyClass_type_var3;

        public static class innerData{
            public String field1;
            public String field2;
            public List<String> field3;
        }       
    }   
}

MyClass cls = new Gson().fromJSon(json, MyClass.class);

Hope this will guide you in de right direction :-)

EDIT:

try this

public class NewClass{
    public List<MyClass> items;
}

NewClass cls = new Gson().fromJSon(json, NewClass.class);

Upvotes: 0

Related Questions