无法将JSON字符串解析为GWT中的Java / JavaScriptObject [英] Can't parse JSON string to Java/JavaScriptObject in GWT

查看:218
本文介绍了无法将JSON字符串解析为GWT中的Java / JavaScriptObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JSON从服务器接收数据,并使用JsonUtils(GWT)进行解析。他们看起来像这样:

  [{id:26,name:Circle1,description:测试,类型:CIRCLE,坐标:[{纬度:50.364736755649716,经度:30.120391845703125}],半径:6577.427847903551,userId:1}] 

我使用这段代码解析它:

  JsArray<地理栅栏> geofenceJsArray = JsonUtils.safeEval(response.getText()); 

但我无法访问坐标列表。当我打电话时

  geofenceJsArray.get(0).getCoordinates()。size()
  Uncaught TypeError:$ getCoordinates_1_g $(...)。size_54_g $不是函数

我做错了什么?

/ generator-throw-an-exception-while-rebinding / 37593073#37593073>生成器在重新绑定时抛出一个异常,您在其中发布了一些源代码:)



您不能将JavaScriptObject的属性设置为非JavaScript类型。 GWT生成的数组几乎看起来像js数组,但是会丢失重要的类型细节,而java.util.List根本无法工作--JS会高兴地假装它会起作用,并且会返回一个JS数组有一个名为 length 属性,但没有方法称为 size code $。
$ b $

改为将 getCoordinates 更改为 JsArray< Coordinate> ;

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!

解决方案

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

这篇关于无法将JSON字符串解析为GWT中的Java / JavaScriptObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆