javascript使用JSON.parse出错

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

问题描述

问 题

完整代码是:

$(document).ready(function() {
    var token = '4e568a7774a5984c4886c35d80d4d5' + '11828c10d8';
    var comments = [],
        reply = [];

    function pull(slug) {
        $.ajax({
            url: 'https://api.github.com/repos/zaaaac/comments/contents/' + slug,
            async: false,
            data: {
                access_token: token
            },
            success: function(data) {
                for (var i = 0; i < data.length; i++) {
                    var download_url = data[i].download_url;
                    $.ajax({
                        url: download_url,
                        async: false,
                        data: {
                            access_token: token
                        },
                        success: function(raw) {
                            if (raw.indexOf('reply') >= 0) {
                                reply.push(raw);
                            } else {
                                comments.push(raw);
                            }
                        }
                    });
                }
            }
        });
    }

    function loadComments() {
        var comments = JSON.parse(comments); 
        $('#comments-count').html('(' + comments.length + ')');
        for (var i = 0; i < comments.length; i++) {
            var id = comments[i]._id,
                name = comments[i].name,
                email = comments[i].email,
                url = comments[i].url,
                content = comments[i].content,
                date = comments[i].date;
            $('ul.comment-list-ul').append('<li id="' + id + '" class="comment"><div class="comment-avatar"><img class="avatar" src="https://secure.gravatar.com/avatar/' + email + '?s=160&amp;d=mm&amp;r=g" height="40" width="40"></div><div class="comment-body"><div class="comment-author"><a href="' + url + '">' + name + '</a></div><div class="comment-content">' + content + '</div><div class="comment-meta clearfix"><span class="comment-date">' + date + '</span></div><div class="comment-children"><ul></ul></div></li>');
        }
    }

    function loadReply() {
        var reply = JSON.parse(reply); 
        for (var j = 0; j < reply.length; j++) {
            var id = reply[j]._id,
                reply = reply[j].reply,
                name = reply[j].name,
                email = reply[j].email,
                url = reply[j].url,
                content = reply[j].content,
                date = reply[j].date;
            $('li#' + reply_id + ' .comment-children ul').append('<li id="' + reply_id + '" class="comment"><div class="comment-avatar"><img class="avatar" src="https://secure.gravatar.com/avatar/' + email + '?s=160&amp;d=mm&amp;r=g" height="40" width="40"></div><div class="comment-body"><div class="comment-author"><a href="' + url + '">' + name + '</a></div><div class="comment-content">' + content + '</div><div class="comment-meta clearfix"><span class="comment-date">' + date + '</span></div></div></li>');
        }
    }
    pull('try');
    loadComments();
    loadReply();
})

comments和reply都可以输出。用document.write输出分别是:

{"_id":"e6e83020-662c-11e7-9b06-05d55a32e74f","name":"me","email":"f4ef777876c6041d07a7e0bd48384e71","url":"","content":"test comment","date":1499772870108},{"_id":"708e1420-6691-11e7-b3ee-2358356fd23d","name":"me","email":"f4ef777876c6041d07a7e0bd48384e71","url":"","content":"another test","date":1499816050567}

{"_id":"29d3ffe0-6691-11e7-b3ee-2358356fd23d","name":"me","email":"f4ef777876c6041d07a7e0bd48384e71","url":"","content":"test reply","reply":"e6e83020-662c-11e7-9b06-05d55a32e74f","date":1499815931907}

使用JSON.parse报错:Uncaught SyntaxError: Unexpected token u in JSON at position 0

求助这是哪里出错了

解决方案

如果返回的数据确定是json格式的,那可以在ajax请求配置中指定为json. 这样的话就不用考虑JSON.parse了.
然后你这里 第一条数据,即不是数组也不是json
JSON.parse是将JSON格式的字符串转换为JSON对象, 调试的时候可以先typeof看看你要转换的数据是什么类型

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

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