解析来自 Servlet 的 JSON 响应
好的,我有一个 Java Servlet 返回一些 JSON(以应用程序/JSON 格式)。为此,我使用 GSON 库。
Servlet GET 方法采用一个参数:ID。 servlet 似乎正在工作,例如,当发送的 [Booking]ID 参数为 1 时,chrome 显示我的 AJAX GET 请求返回以下内容。
0: {WidgetID:46, BookingID:1, X:393, Y:50, Content:Test1}
1: {WidgetID:47, BookingID:1, X:337, Y:251, Content:Test2}
2: {WidgetID:48, BookingID:1, X:97, Y:198, Content:Test3}
我遇到的问题是解析此响应。这是我的JS代码:
<预> 加载位置() { var 预订ID = if (BookingID != null && BookingID != "null") { var data = {"id" : BookingID}; $.getJSON("小部件", 数据, 函数(数据) { // 成功获取所有这些预订小部件作为 JSON,TODO:解析此! }); } }
我应该在“TODO:解析这个!”中添加什么内容?部分? 我想遍历所有元素并获取它们的数据。我真的很讨厌 JQuery 的东西。
Ok, Ive got a Java Servlet returning some JSON (In Application/JSON format). To do this, im using the GSON libary.
The Servlets GET method takes one paramater, ID. The servlet seems to be working, For example,chrome shows my AJAX GET request returning the following when the [Booking]ID paramater sent is 1.
0: {WidgetID:46, BookingID:1, X:393, Y:50, Content:Test1}
1: {WidgetID:47, BookingID:1, X:337, Y:251, Content:Test2}
2: {WidgetID:48, BookingID:1, X:97, Y:198, Content:Test3}
The problem I have is with parsing this response. Here is my JS code:
loadPositions() { var BookingID = if (BookingID != null && BookingID != "null") { var data = {"id" : BookingID}; $.getJSON("Widget", data, function(data) { // Successfully got all this bookings widgets as JSON, TODO: Parse this! }); } }
What should I put in the "TODO: Parse this!" section?
I want to foreach over all the elements, and grab their data. I really suck at this JQuery stuff.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在待办事项部分中,您应该执行以下操作来循环访问所有数组:
In the todo section, you should do the following to loop through all the arrays:
看看 jQuery .each()
http://api.jquery.com/jQuery.each/< /a>
以及您想要执行的操作的一个很好的示例...
http://api.jquery .com/jQuery.getJSON/
Have a look at jQuery .each()
http://api.jquery.com/jQuery.each/
and for a good example of what you want to do...
http://api.jquery.com/jQuery.getJSON/
看看
https://github .com/acobley/jBoggyAppy/blob/HectorV2-Cassandra-0.7.0/WebContent/Scripts/index.js
ShowScrollingTags 函数。
Take a look at
https://github.com/acobley/jBoggyAppy/blob/HectorV2-Cassandra-0.7.0/WebContent/Scripts/index.js
the ShowScrollingTags function.