Can't parse JSON string to Java/JavaScriptObject in GWT

风流意气都作罢 提交于 2019-12-12 03:24:45

问题


I receive data from the server in JSON and parse using JsonUtils (GWT). They look like this:

[{"id":26,"name":"Circle1","description":"Test","type":"CIRCLE","coordinates":[{"latitude":50.364736755649716,"longitude":30.120391845703125}],"radius":6577.427847903551,"userId":1}]

I use this code to parse it:

JsArray<Geofence> geofenceJsArray = JsonUtils.safeEval(response.getText());

But I can't get access to the list of coordinates. When I call

geofenceJsArray.get(0).getCoordinates().size()

I receive this error message in browser's console:

Uncaught TypeError: $getCoordinates_1_g$(...).size_54_g$ is not a function

What I'm doing wrong? Thanks in advance!


回答1:


(From my answer for Generator threw an exception while rebinding, where you posted some of your source:)

You can't have a JavaScriptObject's properties be non-JavaScript types. GWT's generated arrays almost look like js arrays, but will be missing important type details, and java.util.List won't work at all - JS will happily pretend that it will work, and will return a JS array, which has a property called length, but no method called size.

Instead, change your getCoordinates to return JsArray<Coordinate>.



来源:https://stackoverflow.com/questions/37589793/cant-parse-json-string-to-java-javascriptobject-in-gwt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!