Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.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
Javascript 如何提取这个json?_Javascript_Jquery_Json - Fatal编程技术网

Javascript 如何提取这个json?

Javascript 如何提取这个json?,javascript,jquery,json,Javascript,Jquery,Json,我有一个这样的结果 { "Errors" : { "Failure" : { "ShowAsPopup" : true, "ErrorMessage" :"Some Message.", "PopupTitle" : null } }, "IsValid" : false, "WarningMessage" : null, "SuccessMess

我有一个这样的结果

{
    "Errors" : {
        "Failure" : {
            "ShowAsPopup"  : true,
            "ErrorMessage" :"Some Message.",
            "PopupTitle"   : null
        }
    },
    "IsValid" : false,
    "WarningMessage" : null,
    "SuccessMessage" : null
}
现在,如果我执行
Errors.Failure.ShowAsPopup
操作,我会得到一个值。什么是预期的。不过,我想改用索引(如果可能的话)

我试过了

错误[0]。失败。ShowAsPopup
但这只是给了我未定义的错误。理想情况下,我希望将其设置为错误[0].[0]。ShowAsPopup,其中我不必指定“失败”,但可能需要重新考虑该部分

我想要一个通用的方法来处理我的错误。请参阅,有些错误需要弹出窗口,有些只是验证错误。所以现在我把它们都硬编码了,我正试图摆脱这一点。我只是检查这个错误是否需要弹出窗口

所以不是

if(response.Errors.Failure) { // alert('hi')};
else if(response.Errors.Failure2 {// alert('hi2')}

因此,我只需要一个if语句就可以进行检查

大括号{}表示结构而不是数组。所以我认为这应该有效:

Errors.Failure.ShowAsPopup = Errors[0][0] = errors[0].ShowAsPopup
response["Errors"]["Failure"]["ShowAsPopup"]

您的代码和数据必须匹配。如果数据如下所示:

{"Errors":{"Failure":{"ShowAsPopup":true,"ErrorMessage":"Some Message.","PopupTitle":null}},"IsValid":false,"WarningMessage":null,"SuccessMessage":null}
以多行形式显示的,如下所示:

var data = {
    "Errors": {
        "Failure": {
            "ShowAsPopup":true, 
            "ErrorMessage":"Some Message.",
            "PopupTitle":null
        }
    },
    "IsValid":false,
    "WarningMessage":null,
    "SuccessMessage":null
}
if (!data.IsValid) {
    handleError(data);
}

function handleError(info) {
    if (info.Errors ) {
        if (info.Errors.Failure) {
            if (info.Errors.Failure.ShowAsPopup) {
                // show a popup error here using info.Errors.Failure.ErrorMessage
            }
        } else if (/* other types of errors here */) {
            // handle other types of errors here
        }
    }
}
for (var errorName in response.Errors) {
    var error = response.errors[errorName];
    alert(errorName.ErrorMessage);
}
errors[0].Failure.ErrorMessage
//This returns "Some Message" 
errors[1].Failure.ErrorMessage
//returns "Some Other Message" 
然后,您必须使用代码来匹配数据,在本例中是
Errors.Failure.ShowAsPopup
。因为数据是对象形式的,所以必须使用对象语法来读取它。数组和对象不可交换。如果您想使用数组语法来读取数据,那么数据需要是另一个论坛

您可以这样编写一个通用错误函数:

var data = {
    "Errors": {
        "Failure": {
            "ShowAsPopup":true, 
            "ErrorMessage":"Some Message.",
            "PopupTitle":null
        }
    },
    "IsValid":false,
    "WarningMessage":null,
    "SuccessMessage":null
}
if (!data.IsValid) {
    handleError(data);
}

function handleError(info) {
    if (info.Errors ) {
        if (info.Errors.Failure) {
            if (info.Errors.Failure.ShowAsPopup) {
                // show a popup error here using info.Errors.Failure.ErrorMessage
            }
        } else if (/* other types of errors here */) {
            // handle other types of errors here
        }
    }
}
for (var errorName in response.Errors) {
    var error = response.errors[errorName];
    alert(errorName.ErrorMessage);
}
errors[0].Failure.ErrorMessage
//This returns "Some Message" 
errors[1].Failure.ErrorMessage
//returns "Some Other Message" 

您应该执行以下任一操作。首先,您可以简单地循环遍历errors对象,如下所示:

var data = {
    "Errors": {
        "Failure": {
            "ShowAsPopup":true, 
            "ErrorMessage":"Some Message.",
            "PopupTitle":null
        }
    },
    "IsValid":false,
    "WarningMessage":null,
    "SuccessMessage":null
}
if (!data.IsValid) {
    handleError(data);
}

function handleError(info) {
    if (info.Errors ) {
        if (info.Errors.Failure) {
            if (info.Errors.Failure.ShowAsPopup) {
                // show a popup error here using info.Errors.Failure.ErrorMessage
            }
        } else if (/* other types of errors here */) {
            // handle other types of errors here
        }
    }
}
for (var errorName in response.Errors) {
    var error = response.errors[errorName];
    alert(errorName.ErrorMessage);
}
errors[0].Failure.ErrorMessage
//This returns "Some Message" 
errors[1].Failure.ErrorMessage
//returns "Some Other Message" 
这里发生的是response.Errors对象的每个属性

另一个选项是将错误作为数组返回:

{ "Errors": [{"ShowAsPopup": true, "ErrorMessage": "Some message"}] }

任何一种方法都可以很好地工作。

您可以创建一个JSON对象数组

var errors = [
        {"Failure": {
            "ShowAsPopup":true,
            "ErrorMessage":"Some Message.",
            "PopupTitle":"Error Message #1"
            },
        "IsValid":false,
        "WarningMessage":null,
        "SuccessMessage":null
        }, 
        {"Failure": {
            "ShowAsPopup":true,
            "ErrorMessage":"Some Other Message.",
            "PopupTitle":"Error Message #2"
            },
        "IsValid":false,
        "WarningMessage":null,
        "SuccessMessage":null
        }, 
    ]
然后按如下方式访问每个错误:

var data = {
    "Errors": {
        "Failure": {
            "ShowAsPopup":true, 
            "ErrorMessage":"Some Message.",
            "PopupTitle":null
        }
    },
    "IsValid":false,
    "WarningMessage":null,
    "SuccessMessage":null
}
if (!data.IsValid) {
    handleError(data);
}

function handleError(info) {
    if (info.Errors ) {
        if (info.Errors.Failure) {
            if (info.Errors.Failure.ShowAsPopup) {
                // show a popup error here using info.Errors.Failure.ErrorMessage
            }
        } else if (/* other types of errors here */) {
            // handle other types of errors here
        }
    }
}
for (var errorName in response.Errors) {
    var error = response.errors[errorName];
    alert(errorName.ErrorMessage);
}
errors[0].Failure.ErrorMessage
//This returns "Some Message" 
errors[1].Failure.ErrorMessage
//returns "Some Other Message" 

您有一个仅具有字符串属性的对象。这些属性没有顺序,无法通过偏移访问。但是你可以做的是迭代一个对象的属性。不太确定你想说什么?想解释一下吗?我如何使用C#中的集合创建这个结构(我使用mvc Json结果将我的字典转换为Json)无法告诉你。我不知道C#。所以我正在尝试你的for循环。它似乎总是未定义的。它找不到索引或任何奇怪的东西,这似乎不再起作用了。它通过了if语句,但是它说错误总是未定义的,所以它永远不会进入它。