解析JSON字符串返回null [英] Parsing JSON string returns null

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

问题描述

我对JSON处理很新,我自己也陷入了困境。我的页面包含以下脚本;

I'm quite new to JSON handling and I've got myself stuck. My page contains the following script;

<script type="text/javascript">



$(document).ready(function() {

    $('#progressBar').progressbar({value: 0.0});


    process();
});
    function process() {

        getStatus();
        setInterval(getStatus,1000);
    }

    function getStatus() {
        $.getJSON('status-report', function(data) {
            var statusBean = $.parseJSON(data);
              $('#progressBar').progressbar('option','value',$.trim(statusBean.percentComplete));
            $('#status').html(statusBean.statusDescription);
        });
    }


</script>

使用Firebug,我可以看到对'status-report'的调用正在返回一个JSON字符串

Using Firebug, I can see that the call to 'status-report' is returning a JSON string

{"statusBean":{"percentComplete":50.0,"statusDescription":"Default Description"}} 

但在 $。parseJSON 之后,Firebug向我显示变量 statusBean 为空。

but after $.parseJSON, Firebug shows me that the variable statusBean is null.

我做错了什么?

推荐答案

传递给 getJSON 回调的 data 的值,将已经是一个JS对象。 $。parseJSON 将对象传递给它时返回 null ,因为它需要一个字符串。可以在此处简单地删除对 $。parseJSON 的调用,因为 getJSON 首先假设JSON。

The value of data as passed to the getJSON callback, will already be a JS object. $.parseJSON will return null when an object is passed to it, as it expects a string. The call to $.parseJSON can simply be removed here, since getJSON assumes JSON in the first place.

这篇关于解析JSON字符串返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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