Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(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>.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...