如何通过json返回数据
我正在尝试返回有关 guests
的信息(目前只有 id
和 name
),但我不知道如何正确执行它。我的方法如下
由于java不接受关联数组,如何仅将id和名称发送到myData.put()?我尝试在该方法中创建一个要返回的类,但事实证明这在 java 中也是非法的。这里的想法解决方案是什么?
/**
* Retrieves representation of an instance of contentmanagement.ContentManagement
* @return an instance of java.lang.String
*/
@GET @Path("getHtml")
@Produces("application/json")
public String getGuests() {
JSONArray myData = new JSONArray();
for(Guest item : guestDao.getAllGuests()) {
myData.put({});
}
return myData.toString();
}
I'm trying to return information about guests
(only an id
and name
right now) but I can't figure out how to properly do it. My method is below
How can I sent only the id and name to myData.put()
since java doesn't accept associative arrays? I've tried creating a class within this method to be returned but that turns out to be illegal in java as well. What's the idea solution here?
/**
* Retrieves representation of an instance of contentmanagement.ContentManagement
* @return an instance of java.lang.String
*/
@GET @Path("getHtml")
@Produces("application/json")
public String getGuests() {
JSONArray myData = new JSONArray();
for(Guest item : guestDao.getAllGuests()) {
myData.put({});
}
return myData.toString();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JSONObject
类表示名称-值对,因此您的代码如下所示:The
JSONObject
class represents the name-value pair, so your code looks like: