无法将json数组转换为字符串

时间:2013-12-18 06:06:18

标签: jquery ajax json arrays

我没有收到服务器的任何响应,包含以下代码,我收到令牌错误'<'我已经尝试了所有

$(document).ready(function() {
    $.ajax({
        url:"url", 
        dataType: 'json',
        success: function(output) {
            var asd = JSON.stringify(output)
            var i = $.parseJSON(asd);
            for(var j=0;j<i.length;j++) {
                $('#one').append('<p><div>TITLE&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp&nbsp: &nbsp; &nbsp;<a href='+i[j].links+'>'+i[j].Title+'</a><br>SOURCE&nbsp; &nbsp;&nbsp;&nbsp; :  &nbsp; &nbsp;'+i[j].Source+'<br>CATEGORY&nbsp;: &nbsp; &nbsp;'+i[j].Category+'<hr></p></div>');
                //$('#one').append('<p><div style="background-color:#ccc"><span style="font-weight:bold" >SOURCE</span> &nbsp; &nbsp;&nbsp;&nbsp; :  &nbsp; &nbsp;'+i[j].Source+'<p>');
                //$('#one').append('<p><div style="background-color:#ccc" onclick="get"><span style="font-weight:bold" >CATEGORY</span> &nbsp;: &nbsp; &nbsp;'+i[j].Category+'<hr><p></div>');

            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(xhr.statusText);
            alert(thrownError);
        }
    });
});

1 个答案:

答案 0 :(得分:2)

无需调用JSON.stringify()和parseJSON()。如果output是一个数组,你可以直接使用output [0] .Source和output [0] .Category

$.ajax({
url:"url", 
dataType: 'json' ,

success:function(output) {
    for(var j=0;j<output.length;j++) {
        $('#one').append('<p><div>TITLE&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp&nbsp: &nbsp; &nbsp;<a href='+output[j].links+'>'+output[j].Title+'</a><br>SOURCE&nbsp; &nbsp;&nbsp;&nbsp; :  &nbsp; &nbsp;'+output[j].Source+'<br>CATEGORY&nbsp;: &nbsp; &nbsp;'+output[j].Category+'<hr></p></div>');
    }
},
error:function(xhr,ajaxOptions,thrownError){
    alert(xhr.statusText);
    alert(thrownError);
}
});