使用JSweet将地图显示为JSON(Post Map as JSON using JSweet)

编程入门 行业动态 更新时间:2024-10-28 14:28:20
使用JSweet将地图显示为JSON(Post Map as JSON using JSweet)

我想从客户端发送一个java.util.HashMap转换为JSON到服务器。

我使用JSweet为客户端将Java转换为JavaScript。

我看了一下XMLHttpRequest并试图使用JSON.stringify(new HashMap<>())来准备传输映射,但是这导致了

TypeError:循环对象值

在客户端。

这些是我的相关依赖项(使用Gradle):

// Java to JavaScript transpilation compile "org.jsweet:jsweet-transpiler:1.2.0-SNAPSHOT" compile "org.jsweet.candies:jsweet-core:1.1.1" // Allows us to use Java features like Optional or Collections in client code compile "org.jsweet.candies:j4ts:0.2.0-SNAPSHOT"

I'd like to send a java.util.HashMap converted to JSON from the client to the server.

I'm using JSweet to transpile Java to JavaScript for the client side.

I had a look at XMLHttpRequest and tried to prepare the map for transfer using JSON.stringify(new HashMap<>()) but this resulted in a

TypeError: cyclic object value

on the client side.

These are my relevant dependencies (using Gradle):

// Java to JavaScript transpilation compile "org.jsweet:jsweet-transpiler:1.2.0-SNAPSHOT" compile "org.jsweet.candies:jsweet-core:1.1.1" // Allows us to use Java features like Optional or Collections in client code compile "org.jsweet.candies:j4ts:0.2.0-SNAPSHOT"

最满意答案

我必须将java.util.Map转换为jsweet.lang.Object然后使用stringify将其编码为JSON。

以下是使用JSweet将java.util.Map作为JSON发送到服务器的代码:

void postJson(Map<String, String> map, String url) { XMLHttpRequest request = new XMLHttpRequest(); // Post asynchronously request.open("POST", url, true); request.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); // Encode the data as JSON before sending String mapAsJson = JSON.stringify(toJsObject(map)); request.send(mapAsJson); } jsweet.lang.Object toJsObject(Map<String, String> map) { jsweet.lang.Object jsObject = new jsweet.lang.Object(); // Put the keys and values from the map into the object for (Entry<String, String> keyVal : map.entrySet()) { jsObject.$set(keyVal.getKey(), keyVal.getValue()); } return jsObject; }

像这样使用它:

Map<String, String> message = new HashMap<>(); message.put("content", "client says hi"); postJson(message, "http://myServer:8080/newMessage");

I had to convert the java.util.Map to a jsweet.lang.Object before encoding it as JSON using stringify.

Here's the code to send a java.util.Map as JSON to the server using JSweet:

void postJson(Map<String, String> map, String url) { XMLHttpRequest request = new XMLHttpRequest(); // Post asynchronously request.open("POST", url, true); request.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); // Encode the data as JSON before sending String mapAsJson = JSON.stringify(toJsObject(map)); request.send(mapAsJson); } jsweet.lang.Object toJsObject(Map<String, String> map) { jsweet.lang.Object jsObject = new jsweet.lang.Object(); // Put the keys and values from the map into the object for (Entry<String, String> keyVal : map.entrySet()) { jsObject.$set(keyVal.getKey(), keyVal.getValue()); } return jsObject; }

Use it like this:

Map<String, String> message = new HashMap<>(); message.put("content", "client says hi"); postJson(message, "http://myServer:8080/newMessage");

更多推荐

本文发布于:2023-08-04 07:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1413197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:地图   JSweet   JSON   Map   Post

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!