解析通过JavaScript的JSON对象(对象列表)和环路的呢? [英] Parsing a JSON object (list of object) through Javascript and loop in it?

查看:152
本文介绍了解析通过JavaScript的JSON对象(对象列表)和环路的呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道有一些错误,但我不明白的东西。

Ok, I know there's something wrong but I can't understand what.

我阅读服务从我写了一个Web服务的一个ArrayList(在JSON的MediaType)。 去该地址返回我的JSON字符串。

I read an ArrayList of Service (in JSON mediatype) from a web service that I wrote. Going to that address returns me the json string.

现在我试图做一个网页,用于显示值,看到制作的页面请求,每3秒的变化。

Now I'm trying to make a web page for showing that values and seeing the changes making to the page a request every 3 secs.

我怎样才能解析它,或者用它?阅读大量的多,我还是要开始..

How can I parse it, or use it? Read lot and more and I'm still to the start..

[是不是可以到这个对象传递给JSP和解析,循环,一切与JSTL?这将是真棒!]

[Is not possible to pass this object to the JSP and parse, loop and everything with JSTL? That would be awesome!]

下面的JS code:

Here the js code:

 <script type="text/javascript">
setInterval(function(){
    $.ajax({ url: "/MyApp/rest/display", success: function(data){
        var objs = $.parseJSON(data);
        $.each(objs, function(i,service) {
            $("#service").append('<p>'+service+'</p>');
        });
    }, dataType: "json"});
}, 3000);
</script>

我有一个&LT; D​​IV ID =服务&GT;

编辑: 几乎没有!

现在我有这样的:

<script type="text/javascript">
setInterval(function(){
    $.ajax({ url: "/myApp/rest/display", success: function(data){
        $.each(data, function(i,service) {
            var cont = 1;
            var newdiv = document.createElement('div');
            newdiv.setAttribute('id', "service"+i);
            $("#service"+i).html('<p>'+service.serviceId+" "+service.queue.lastNumber+'</p>');
            document.getElementById("services").appendChild(newdiv);
            cont++;
        });
    }, dataType: "json"});
}, 5000);
</script>

它得到了更新和所有,但我有一个问题(太好了!):它不断创造新的div 的内部更大的一个(空的div)。我怎样才能避免这种情况?

It gets the updates and all (nice!) but I've one problem: it keeps creating new divs inside the bigger one (empty divs). How can I avoid this?

EDIT2:

没关系,我已经做了! 就在之前的appendChild加入这一行:

Nevermind, I've done! Just add this line before the appendChild:

if(!$("#service"+i).length)

工程就像一个魅力。谢谢!

Works like a charm. Thanks!

推荐答案

与jQuery你不需要解析数据,因为它是在设置已经被解析为JSON在数据类型:JSON

With JQuery you do not need to parse the data since it is already parsed as JSON when you set the dataType: "json".

$.ajax({ 
    url: "/MyApp/rest/display", 
    success: function(data) {
        $.each(data, function(i,service) {
            $("#service").append('<p>'+service+'</p>');
        });}, 
    dataType: "json" });

PS。如果还是不行,请邮寄正由您的AJAX调用返回的JSON。

PS. If that doesn't work, please post the JSON that is being returned by your AJAX call.

编辑:这是做事的JSTL方式: 创建JSF或JSP不返回一个完整的HTML页面,但你想要的#服务 DIV里面有什么。让我们称之为页面 doit.jsp 。现在,我们可以只使用AJAX来放进了#服务 DIV。

Here is the JSTL way of doing things: Create a JSF or JSP that does NOT return a full HTML page but just what you want inside the #service div. Lets call the page doit.jsp. Now we can just use ajax to put that in the #service div.

$.ajax({
    url: "doit.jsp",
    success: function(data) { $("#service").html(data); });

这篇关于解析通过JavaScript的JSON对象(对象列表)和环路的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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