Tibbers
Tibbers

Reputation: 131

GSON parsing fromJSON regarding arrays of objects exception

I am using google's GSON parsing library and I'm not sure if this has been asked before (I did check what I could find) but, here I go!! It's regarding the fromJSON method and how it refuses to interpret some classes as arrays.

Given the JSON structure below:

{"visualization": {
"root": {
    "fullname": "CC/dudu",
    "name": "dudu",
    "type": "String",
    "children": [
        {
            "fullname": "CC/dudu/lulu",
            "name": "lulu",
            "type": "String"
        }
    ]
},
"traces": {
    "messages": [
        {
            "from": "dudu",
            "method": "call()",
            "scenario": "#1",
            "timestamp": "09-12-2013 00:21:14",
            "to": "dudu",
            "type": "void",
            "violation": "true",
            "visible": "true"
        }
    ],
    "scenarios": [
        {
            "name": "testscenario",
            "id": "#1",
            "description": "testing parsing!"
        }
    ]
}
}}

And the accompanying contained classes.

public class Response {
private Visualization visualization;
    //+getter/setter
}

public class Visualization {
private Component root;
private Map<String, Trace> traces;
    //+getter/setter
} 

public class Trace {
private ArrayList<Message> messages;
private ArrayList<Scenario> scenarios;
    //+getter/setter
}

I get the error that GSON was expecting an Object and NOT an Array (before the "[" token of messages). Anyone know why that is? The types (as can be seen in the classes) are List, so it should be fine. And I have tried having more objects in the array and I still get the same error message! Why is Gson interpreting the List<TypeA> type as an object and not an array?

EDIT: Here's the code code, but it's kinda pointless, since the exception is being thrown because of the parsing process. I doubt you'll find anything useful. "visualization" is a string with a correct JSON format.

Gson gsonParser = new Gson();
Response r = gsonParser.fromJson(visualization, Response.class);

Upvotes: 2

Views: 706

Answers (1)

giampaolo
giampaolo

Reputation: 6934

The code below allows you to parse exactly your JSON.

Note that I put all into a class to make easier for you to test it. Anycase, Gson does not work well with inner classes unless they are static. So I suggest you to make a file for each class or use only static inner classes.

package stackoverflow.questions;

import java.util.*;

import com.google.gson.Gson;

public class Q20461706 {

   public class Trace {
      ArrayList<Message> messages;
      ArrayList<Scenario> scenarios;
      @Override
      public String toString() {
         return "Trace [messages=" + messages + ", scenarios=" + scenarios + "]";
      }

   }

   public static class Message {
      String from; // "from": "dudu",
      String method; // "method": "call()",
      String scenario; // "scenario": "#1",
      String timestamp; // "timestamp": "09-12-2013 00:21:14",
      String to; // "to": "dudu",
      String type; // "type": "void",
      Boolean violation; // "violation": "true",
      Boolean visible; // "visible": "true"
      @Override
      public String toString() {
         return "Message [from=" + from + ", method=" + method + ", scenario=" + scenario + ", timestamp=" + timestamp + ", to=" + to + ", type=" + type + ", violation=" + violation + ", visible=" + visible + "]";
      }


   }

   public static class Scenario {
      String name;// "name": "testscenario",
      String id; // "id": "#1",
      String description; // "description": "testing parsing!"
      @Override
      public String toString() {
         return "Scenario [name=" + name + ", id=" + id + ", description=" + description + "]";
      }

   }

   public static class Component {
      String fullname; // "fullname": "CC/dudu",
      String name; // "name": "dudu",
      String type; // "type": "String",
      List<Component> children;
      @Override
      public String toString() {
         return "Component [fullname=" + fullname + ", name=" + name + ", type=" + type + ", children=" + children + "]";
      }

   }

   public static class Visualization {
      Component root;
      Trace traces;
      @Override
      public String toString() {
         return "Visualization [root=" + root + ", traces=" + traces + "]";
      }

   }

   public static class Response {
      Visualization visualization;

      @Override
      public String toString() {
         return "Response [visualization=" + visualization + "]";
      }

   }

   public static void main(String[] args) {
      String json = 
     " {\"visualization\": {                                   "+
     "    \"root\": {                                          "+
     "        \"fullname\": \"CC/dudu\",                       "+
     "        \"name\": \"dudu\",                              "+
     "        \"type\": \"String\",                            "+
     "        \"children\": [                                  "+
     "            {                                            "+
     "                \"fullname\": \"CC/dudu/lulu\",          "+
     "                \"name\": \"lulu\",                      "+
     "                \"type\": \"String\"                     "+
     "            }                                            "+
     "        ]                                                "+
     "    },                                                   "+
     "    \"traces\": {                                        "+
     "        \"messages\": [                                  "+
     "            {                                            "+
     "                \"from\": \"dudu\",                      "+
     "                \"method\": \"call()\",                  "+
     "                \"scenario\": \"#1\",                    "+
     "                \"timestamp\": \"09-12-2013 00:21:14\",  "+
     "                \"to\": \"dudu\",                        "+
     "                \"type\": \"void\",                      "+
     "                \"violation\": \"true\",                 "+
     "                \"visible\": \"true\"                    "+
     "            }                                            "+
     "        ],                                               "+
     "        \"scenarios\": [                                 "+
     "            {                                            "+
     "                \"name\": \"testscenario\",              "+
     "                \"id\": \"#1\",                          "+
     "                \"description\": \"testing parsing!\"    "+
     "            }                                            "+
     "        ]                                                "+
     "    }                                                    "+
     "    }}                                                   ";

      Gson gsonParser = new Gson();

      Response r = gsonParser.fromJson(json, Response.class);

      System.out.println(r);


   }

}

To respond to your question, note that I changed your Visualization class with Trace traces since you have only a Trace object in your JSON and not an array. This is why Gson complains.

Note also that I avoided to parse the date as date, you need to specify a custom date format for that, but it's beyond the scope of this question. You can find many examples here on SO.

Upvotes: 1

Related Questions