将嵌套数据结构从 JavaScript 传递到小程序
在我的页面中有一个小程序。我想将一些数据传递给小程序函数。我已经找到了如何做到这一点,并且它对于像字符串这样的简单数据类型工作得很好,但是我在处理一些嵌套数据结构时遇到了困难。我不确定什么是正确的术语——字典列表或关联数组的数组。这是我构建的 JavaScript 数据结构的片段。
var servers = []
servers.push({'id' : 1, 'ip' : '111.111.111.111'});
servers.push({'id' : 2, 'ip' : '222.222.222.222'});
$('testapplet').setWork(servers);
我如何在 Java 中访问它?我想迭代它们并将它们打印到控制台?我尝试了以下代码,但无法让它工作。
public void setWork(HashMap s[]) {
for (int i = 0; i < s.length; i++) {
HashMap item = s[i];
System.out.println(item);
System.out.println(item.get("ip"));
}
}
In my page I have an applet. I'd like to pass a some data to an applet function. I've found out how to do this and it worked fine for simple datatypes like strings but I'm having hard time with some nested data structures. I'm not sure what would be the right term for it — a list of dictionaries or an array of associative arrays. Here's a snippet of my JavaScript data structure is built.
var servers = []
servers.push({'id' : 1, 'ip' : '111.111.111.111'});
servers.push({'id' : 2, 'ip' : '222.222.222.222'});
$('testapplet').setWork(servers);
How do I access this in Java? I'd like to iterate over them and print them to the console? I tried the following code but I couldn't get it to work.
public void setWork(HashMap s[]) {
for (int i = 0; i < s.length; i++) {
HashMap item = s[i];
System.out.println(item);
System.out.println(item.get("ip"));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定 Java/JavaScript 桥是否支持原始类型以外的任何内容。
您可以尝试序列化为 JSON,然后通过桥传递:
编辑:正如刚才提到的...:)
I'm not sure the Java/JavaScript bridge support anything other than primitive types.
You could try serialising to JSON, then pass that over the bridge:
Edit: as just mentioned... :)
也许有更好的解决方案,但您可以在 Javascript 中对数据进行 JSON 字符串化,然后在 Java 中对其进行解码。
There maybe a better solution, but you could JSON stringify the data in Javascript and then decode it in Java.