[英]How to return error string from json object without the first element
This is my json object 这是我的json对象
{"result":0,
"last-name":'please enter your last name',
"quantity":'quantity must be a number',
"email": 'please enter a valid email '}
I need the element "result":0, because it lets me know there is an error, so that I can then pass it to an error string. 我需要元素“ result”:0,因为它让我知道存在错误,以便可以将其传递给错误字符串。
However I need to remove the 0 from the error output string because the end user only needs the error messages when they are outputted. 但是,我需要从错误输出字符串中删除0,因为最终用户仅在输出错误消息时才需要它们。 I have tried to change the object to an array and also tried few array methods such as pop() and slice() but I am having trouble putting them into action. 我试图将对象更改为数组,还尝试了一些数组方法,例如pop()和slice(),但我无法将它们付诸实践。 I think I may need to loop through the array but this seems over kill as I know that I only want to remove the 'result' element which is always at the start. 我想我可能需要遍历数组,但是这似乎是致命的,因为我知道我只想删除始终在开头的“结果”元素。
This is how I am returning the error array: 这就是我返回错误数组的方式:
var errorString = '<div class="alert alert-danger">';
errorString += '<table >';
for( key in o) {
errorString += '<tr>';
errorString += '<td>';
errorString += o[key] ;
errorString += '</tr>';
// alert( " " + [ key ] + ", " + o[ key ] );
}
errorString += '</table>' ;
errorString += '</div>' ;
$(".info").append(errorString);
$(function() {
$('.info').delay(1000).fadeOut('.info', function() {
$('.info').empty();
$('.info').show();
});
});
Try like this 这样尝试
var obj = {"result":0,
"last-name":'please enter your last name',
"quantity":'quantity must be a number',
"email": 'please enter a valid email '}
obj.result returns the "result" key only
delete obj.result will return the object without the result key
Then you can iterate or do what you want. 然后,您可以迭代或执行所需的操作。
Try this: Which uses jQuery $.each() method 试试这个:使用jQuery $ .each()方法
var o = {
"result":0,
"last-name":'please enter your last name',
"quantity":'quantity must be a number',
"email": 'please enter a valid email '};
var errorString = '<table >';
$.each(o, function(i, key){
if(i != "result")// check for result
{
errorString += '<tr>';
errorString += '<td>';
alert(key);
errorString += key ;
errorString +='</td>';
errorString +='</tr>';
}
});
errorString += '</table>';
If you have a string, then do a JSON.parse
to convert to a json object. 如果您有字符串,请执行JSON.parse
转换为json对象。 Otherwise first check if there is an error, if yes then iterate the object after deleting the first property. 否则,请首先检查是否存在错误,如果是,则在删除第一个属性后对对象进行迭代。
Demo: http://jsfiddle.net/abhitalks/NE5v5/ 演示: http : //jsfiddle.net/abhitalks/NE5v5/
Example Code: 示例代码:
var s = '{"result":0, "last-name":"please enter your last name", "quantity":"quantity must be a number", "email":"please enter a valid email"}' ;
var j = JSON.parse(s); // If a string, first make a json object
if (! j.result) { // check if there is an error, 0 means false
delete j.result; // if there is an error, delete the first property
for(var key in j) { // iterate the object properties
// key contains the property name
// j[key] contains the property value
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.