简体   繁体   English

JSON.parse没有错误但无法正常工作

[英]JSON.parse not erroring but not working

I am working on my webdesign assignment based around ajax. 我正在基于ajax进行Web设计工作。 I am having a problem with the JSON.parse() function. 我的JSON.parse()函数有问题。 The problem goes as follows, I perform a get request on my JSON database using ajax: 问题如下,我使用ajax在JSON数据库上执行get请求:

artist_list = $.ajax({
    dataType: "json",
    async: false,
    method: "GET",
    url: "/database/artists.json",
    error: function (xhr) {
        console.log("AJAX error:", xhr.status);
    },
    success: function(xhr, responseText) {
        console.log("Ajax operation succseded the code was:", xhr.status);
        console.log("This is the output", responseText);
        response = responseText;
        console.log("Parsing artist list");
        JSON.parse(response);
        console.log("Artist list parsed");
    },
    done: function(data, textStatus, jqXHR){
        console.log("data:", data);
        console.log("Response text",jqXHR.responseText);
    }
})

The console log of the responseText matches what the JSON file I wanted but, when I run response text through a for loop the log returns the JSON file char by char: responseText的控制台日志与我想要的JSON文件匹配,但是,当我通过for循环运行响应文本时,该日志按char返回JSON文件char:

artist_list = artist_list.responseText;
JSON.parse(artist_list);
console.log(artist_list);
for(var i = 0; i < 1; i++) {
    console.log("generating artist,", i);
    console.log("which is:", artist_list[i]);
    index = parseInt(i);
    index = new artist_lite(artist_list[i]);
    artist_lite_dict.push(index);
}

The console returns: 控制台返回:

generating artist, 0
which is: {     

The list is limited to one because of the lenght of the JSON object which I am trying to pass through. 由于我要传递的JSON对象的长度,该列表限制为一个。 The JSON can be found here https://github.com/Stephan-kashkarov/Collector/blob/master/main/database/artists.json along with the whole code for testing purposes. JSON可以在这里找到https://github.com/Stephan-kashkarov/Collector/blob/master/main/database/artists.json以及用于测试目的的整个代码。

Thanks for your help! 谢谢你的帮助! - Stephan -斯蒂芬

You need to save the result of JSON.parse(artist_list); 您需要保存JSON.parse(artist_list);的结果JSON.parse(artist_list); into some variable. 变成一些变量 I guess in your code you would like to solve it like this: 我想在您的代码中您想这样解决:

artist_list = JSON.parse(artist_list);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM