简体   繁体   English

无法将JSON字符串解析为GWT中的Java / JavaScriptObject

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

I receive data from the server in JSON and parse using JsonUtils (GWT). 我从服务器接收JSON数据,并使用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! 提前致谢!

(From my answer for Generator threw an exception while rebinding , where you posted some of your source:) (根据我对Generator的回答, 重新绑定时引发了异常 ,您在其中发布了一些来源:)

You can't have a JavaScriptObject's properties be non-JavaScript types. 您不能让JavaScriptObject的属性为非JavaScript类型。 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 . GWT生成的数组看起来几乎像js数组,但是将丢失重要的类型细节,并且java.util.List根本不起作用-JS会很高兴地假装它会工作,并会返回具有属性的JS数组称为length ,但没有方法称为size

Instead, change your getCoordinates to return JsArray<Coordinate> . 相反,将您的getCoordinates更改为返回JsArray<Coordinate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM