I can't seem to locate the proper way to use the JSON parser when the result is a pure array with no top level tagging.
Here is an example. I have a REST API.
The API defines a method GET /getrecentactivity
The result is a pure javascript array that looks exactly like this...
[
{
"account": "user1",
"category": "broadcast",
"msgid": "some unique id",
"time": 1395920488,
"timereceived": 1395920488,
"message": "Hey everyone this a broadcast!"
},
{
"account": "user1",
"category": "direct",
"destination": "user2",
"msgid": "some other unique id",
"time": 1395920492,
"timereceived": 1395920493,
"message": "Hello user2! How is life?"
}
]
The content type is application/json
To access this RESTful API I'm using a ConnectionRequest with a ResponseListener (extends ActionListener) that looks like this.
public void actionPerformed(ActionEvent evt) {
Result result;
try{
result = Result.fromContent(new String(request.getResponseData()), Result.JSON);
Application.setActivityList(result);
}catch(IllegalArgumentException ex){
Display.getInstance().callSerially(new Runnable(){
public void run() {
Dialog.show("Error!", "The server returned an invalid result.\nPlease try again later.", "OK", null);
}
});
}
}
Application.setActivityList(result) looks like
public static void setActivityList(Result result) {
recentActivityList = result.getAsArray("/");
}
Unfortunately that line "recentActivity = result.getAsArray("/");" throws the following exception
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:658)
at com.codename1.processing.ResultTokenizer.next(ResultTokenizer.java:146)
at com.codename1.processing.ResultTokenizer.tokenize(ResultTokenizer.java:77)
at com.codename1.processing.Result._internalGetAsArray(Result.java:861)
at com.codename1.processing.Result.getAsArray(Result.java:844)
at com.cannibay.potwallet.Application.setActivityList(Application.java:123)
I've checked the Network Monitor and the result is exactly what I pasted above. I've ran it through a JSON validator and it validates fine. Furthermore I have a webpage with a javascript element that does a JSON.parse on this same REST call and it gives me an array every time.
So at this point I'm pretty well stumped. It seems that / should be the root of the JSON response but clearly the JSON parse doesn't like it.
Any ideas?