Reputation: 1031
I have a JSON response like this:
{"error":true,"variable":{"v_f1_email":["error-mail","mail"]}}
I am trying to access 'error-mail' and 'mail' like this:
var JSD = JSON.parse(data);
if (JSD.error == true) {
$(JSD.variable).each(function(index, el) {
var error = el[0];
var type = el[0];
console.log(error + type);
})
Unfortunately the vars error
and type
are returning undefined
. Where am I wrong? Thanks
Upvotes: 1
Views: 60
Reputation: 177950
I believe you are looking for this:
Note: I renamed the vars in the each to show it is not an indexed array but key/value
// var JSD = JSON.parse(data); // version to use in your page
var JSD = {"error":true,"variable":{"v_f1_email":["error-mail","mail"]}};
if (JSD.error) {
$.each(JSD.variable,function(fieldName,errorArr) {
$("#error").append(fieldName+": error:"+errorArr[0]+", type:"+errorArr[1]);
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="error"></div>
Upvotes: 2