GWT HashMap到/从JSON [英] GWT HashMap to/from JSON

查看:89
本文介绍了GWT HashMap到/从JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今晚我可能会有点疲倦,但是现在看来:

我希望GWT HashMap来自/来自JSON。我将如何实现这一目标?



换句话说,我想要一个 HashMap ,取其JSON表示,将其存储在某个地方并将其恢复到其原生的Java表示形式。 解决方案

不是最优化的,但应该很容易代码:使用 JSONObject



迭代您的地图条目并 put > JSONObject (将每个值转换为适当类型的 JSONValue ),然后调用 toString 来获取JSON表示。



解析时,获取 JSONObject 返回使用 JSONParser ,然后遍历 keySet get 将这些值放入地图中(解开 JSONValue s之后)

但要小心你使用的钥匙!您不能在JS中使用任何类型的键作为属性名称;并且浏览器中的JSON处理总是涉及通过JS对象(或者自己实现JSON解析器,这不会执行相同的操作)。

I might be getting a bit tired tonight but here it goes:

I'd like to have GWT HashMap to/from JSON. How would I achieve this?

In other words, I'd like to take an HashMap, take its JSON representation, store it somewhere and get it back to its native Java representation.

解决方案

Not the most optimized, but should be easy to code: use JSONObject.

Iterate over your map's entries and put them in a JSONObject (converting each value to a JSONValue of the appropriate type), then call toString to get the JSON representation.

For parsing, get a JSONObject back using a JSONParser, then iterate over the keySet, geting the values and putting them in your map (after unwrapping the JSONValues)

But beware of the keys you use! You cannot use any kind of key as a property name in JS; and JSON processing in the browser always involve going through a JS object (or implementing the JSON parser yourself, which won't perform the same)

这篇关于GWT HashMap到/从JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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