I am strugling with my first Ckan application and from the reading i did i choose to use Gson. From Ckan, for testing, i try to get the user list by http://192.168.1.2:5000/api/action/user_list
This gives me
{"help":"http://192.168.1.2:5000/api/3/action/help_show?name=user_list",
"success":true,
"result":[
{ "openid":null,
"about":null,
"display_name":"default",
"name":"default",
"created":"2015-06-09T22:17:22.228196",
"email_hash":"d41d8cd98f00b204e9800998ecf8427e",
"sysadmin":true,
"activity_streams_email_notifications":false,
"state":"active",
"number_of_edits":0,
"fullname":null,
"id":"d5e49a3d-599d-49f3-9e20-826a03540357",
"number_created_packages":0
}, (..more data here...deleted for convenience)
{ "openid":null,
"about":null,
"display_name":"visitor",
"name":"visitor",
"created":"2015-06-09T22:16:52.785325",
"email_hash":"d41d8cd98f00b204e9800998ecf8427e",
"sysadmin":false,
"activity_streams_email_notifications":false,
"state":"active",
"number_of_edits":0,
"fullname":null,
"id":"9d279d8a-c068-46a5-9516-ed9c8de2f13b",
"number_created_packages":0
}
]
}
My problem is how to read this using Java. What I did was
class JsonUserReply{
public String help;
public boolean success;
public JsonUsers[] userArray;
JsonUserReply(){}
}
class JsonUsers{
public String openid;
public String about;
public String display_name;
public String name;
public String created;
public String email_hash;
public boolean sysadmin;
public boolean act_email_notif;//"activity_streams_email_notifications"
public String state;
public int edits;
public String fullname;
public String id;
public int numb_cre_packs;//"number_created_packages"
JsonUsers(){}
}
I use the first class because the data provided is "some data, {array of Objects}".
Folowing some tutorials i used this:
Gson gson = new Gson();
Type collectionType = new TypeToken<Collection<JsonUserReply>>(){}.getType();
Collection<JsonUserReply> enums = gson.fromJson(host, collectionType);
This gives me an error on the fromJson
command of:
Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
and the same error i get using this
JsonUserReply myTypes = gson.fromJson(host,JsonUserReply.class);
Edit
JsonUserReply myTypes = gson.fromJson(host,JsonUserReply.class);
throws Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
while digging around i found and used this*
url = new URL(host); //("https://graph.facebook.com/search?q=java&type=post");
InputStream is = url.openStream();
JsonParser parser = Json.createParser(is);
while (parser.hasNext()) {
Event e = parser.next();
if (e == Event.KEY_NAME) {
switch (parser.getString()) {
case "name":
parser.next();
System.out.println("Name "+parser.getString());
break;
case "result":
System.out.println("RESULT1 "+parser.getString());
e=parser.next();
System.out.println("RESULT2 "+e.toString());
break;
default:
System.out.println(parser.getString());
break;
}
}
which even if it is way too complicated to use for my data as they are, it revieled something intresting. inside the case "result":
i re-read the parser just to check whats its value. And it turns out that
RESULT1 result
RESULT2 START_ARRAY
RESULT3 START_OBJECT
To me that means that the json object is sending the data with the keyword"START_ARRAY" but Gson is expecting BEGIN_ARRAY. Am i right? apparently these two are not the same. how can i use the Gson way to get the array from my data? Thanks