使用JSON返回Java Map [英] Using JSON to return a Java Map

查看:390
本文介绍了使用JSON返回Java Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JSON格式返回Java Map的最佳方法是什么? 我的具体需求是日期和数字之间的键->值关联.

What's the best way to return a Java Map using the JSON format? My specific need is a key -> value association between a date and a number.

我的担心是:我的结构基本上包含N个相同类型的元素(日期和数字之间的映射),并且我希望能够在Javascript中快速遍历它们.

My concern is this: My structure basically contains N elements of the same type (a mapping between a date and a number), and I want to be able to quickly iterate through them in Javascript.

在XML中,我将拥有:

In XML, I would have:

<relation date='mydate' number='mynumber'/>
<relation date='mydate' number='mynumber'/>
...
<relation date='mydate' number='mynumber'/>

然后我会像这样使用jQuery:

and I would use jQuery like this:

$(xml).find("relation").each(function() {
    $(this).attr("date"); // the date
    $(this).attr("number"); // the number
})

这是我第一次使用JSON,我想知道我是否可以做类似的事情.

It's my first experience with JSON and I would like to know if I can do something similar.

推荐答案

尽管我自己还没有尝试过,但 JSONObject JSON的JSON的Java实现.org 具有 构造函数.

Although I haven't tried it myself, the JSONObject of the Java implementation of JSON from json.org has a JSONObject(Map) constructor.

一旦从Map创建了JSON对象,则可以通过调用

Once a JSON object is created from the Map, then a JSON string can be obtained by calling the toString method.

这篇关于使用JSON返回Java Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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