Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过JSON对象循环的JavaScript/jQuery解决方案_Javascript_Jquery_Ajax_Json - Fatal编程技术网

通过JSON对象循环的JavaScript/jQuery解决方案

通过JSON对象循环的JavaScript/jQuery解决方案,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,我从PHP发送了这种JSON: {"status":"error","message":"Firstname is invalid"}{"status":"error","message":"Lastname is invalid"}{"status":"success","message":"Middle name is fine"} Ajax让我想起了成功的故事: success:function(data){ data=JSON.parse(data); //need to loop t

我从PHP发送了这种JSON:

{"status":"error","message":"Firstname is invalid"}{"status":"error","message":"Lastname is invalid"}{"status":"success","message":"Middle name is fine"}
Ajax让我想起了成功的故事:

success:function(data){
data=JSON.parse(data);
//need to loop trough data here
}
所以问题是,我需要立即console.logdata.status/*和*/data.message。 忽略上面的评论

因此,在上面的JSON示例中,我希望能够将以下内容记录到console.log中:

1错误名称无效 2错误Lastname无效 3.中间名可以

上面的数字只是指它在chrome中的控制台上的样子。我不需要给它们编号

////////


我实际上试图完成的是根据status.value wither success或error使用alrtify.js显示成功/错误消息。我不想在一个通知中显示它们。我希望每个错误/成功都显示为单独的通知。为此,我需要对它们进行循环。

假设您实际接收的是一个数组,而不是您在那里显示的无效JSON:

success:function(data){
    data=JSON.parse(data); // you probably shouldn't be using this line
    data.forEach(function (item) {
        console.log(item.status + ' ' + item.message);
    });
}

这个密码对我来说很有魅力。 请注意,在我的字符串中,对象是用逗号分隔的,而不是您的“{status:error,message:Firstname无效}{status:error,message:Lastname无效}{status:success,message:Middle name很好}”

var data = '[{"status":"error","message":"Firstname is invalid"}, {"status":"error","message":"Lastname is invalid"}, {"status":"success","message":"Middle name is fine"}]';

var parsedData = JSON.parse(data);

parsedData.forEach(function(item, index) {
    alert(item.status + " - " + item.message);
    console.log(item.status + " - " + item.message);
});

请查看您的JSON格式。应该是这样的:

[{"status":"error","message":"Firstname is invalid"},{"status":"error","message":"Lastname is invalid"},{"status":"success","message":"Middle name is fine"}];
因此,最终您将能够循环

var data = [{"status":"error","message":"Firstname is invalid"},    
            {"status":"error","message":"Lastname is invalid"},    
            {"status":"success","message":"Middle name is fine"}];

for(var i in data)
{
 console.log(data[i].message)
}

这是无效的JSON文本,调用parse时应该会出现错误
var data = [{"status":"error","message":"Firstname is invalid"},    
            {"status":"error","message":"Lastname is invalid"},    
            {"status":"success","message":"Middle name is fine"}];

for(var i in data)
{
 console.log(data[i].message)
}