1

I wrote code to parse response from server in json format. Here is the json

{
    "2FrontSide": {
        "back": ""
    },
    "3BackSide": {
        "back": ""
    },
    "4FrontSide": {
        "back": {
            "type": "image",
            "url": "http:\/\/www.abc.com\/sample_50.png"
        }
    }
}

and the code to parse this response is

Type collectionType = new TypeToken<Map<String,Map<String, VideosImages>>>(){}.getType();
Map<String,Map<String, VideosImages>> data = json.fromJson(resp, collectionType);

It gives

10-23 17:50:25.500: W/System.err(29598): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 33
10-23 17:50:25.500: W/System.err(29598):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:180)
10-23 17:50:25.500: W/System.err(29598):    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
10-23 17:50:25.505: W/System.err(29598):    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:188)
10-23 17:50:25.505: W/System.err(29598):    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:146)
10-23 17:50:25.505: W/System.err(29598):    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
10-23 17:50:25.505: W/System.err(29598):    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:188)
10-23 17:50:25.505: W/System.err(29598):    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:146)
10-23 17:50:25.510: W/System.err(29598):    at com.google.gson.Gson.fromJson(Gson.java:755)
10-23 17:50:25.510: W/System.err(29598):    at com.google.gson.Gson.fromJson(Gson.java:721)
10-23 17:50:25.510: W/System.err(29598):    at com.google.gson.Gson.fromJson(Gson.java:670)
10-23 17:50:25.515: W/System.err(29598):    at com.putitout.buck.helpers.Network.fetchAllVideosAndImages(Network.java:47)
10-23 17:50:25.515: W/System.err(29598):    at com.putitout.buck.VideoPlayback$2.run(VideoPlayback.java:462)
10-23 17:50:25.520: W/System.err(29598):    at java.lang.Thread.run(Thread.java:856)
10-23 17:50:25.520: W/System.err(29598): Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 33
10-23 17:50:25.525: W/System.err(29598):    at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
10-23 17:50:25.525: W/System.err(29598):    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
10-23 17:50:25.530: W/System.err(29598):    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:168)
10-23 17:50:25.530: W/System.err(29598):    ... 12 more

1 Answer 1

2

Implement InstanceCreator<Resp> and JsonDeserializer<Resp> to your resp and collection.

Register Type adapter in GsonBuilder

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter();

and Parse your json context in deserialize method.

    @Override
    public YourClass deserialize(JsonElement json, Type typeOfT,
            JsonDeserializationContext context){

}
Sign up to request clarification or add additional context in comments.

4 Comments

GsonBuilder is inside jar file. How do I register Type adapter there?
Resp is just a string, Should I implement these two interfaces at the class where I am using resp?
No ,the just collection or your class VideoImages
"How do I register Type adapter there?" Look here: google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.