繁体   English   中英

如何从没有第一个元素的json对象返回错误字符串

[英]How to return error string from json object without the first element

这是我的json对象

                {"result":0,
                  "last-name":'please enter your last name',
                  "quantity":'quantity must be a number',
                  "email": 'please enter a valid email '}

我需要元素“ result”:0,因为它让我知道存在错误,以便可以将其传递给错误字符串。

但是,我需要从错误输出字符串中删除0,因为最终用户仅在输出错误消息时才需要它们。 我试图将对象更改为数组,还尝试了一些数组方法,例如pop()和slice(),但我无法将它们付诸实践。 我想我可能需要遍历数组,但是这似乎是致命的,因为我知道我只想删除始终在开头的“结果”元素。

这就是我返回错误数组的方式:

 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();
                                                                              });
                                  });

尝试这个:

var jsonObj = {"result":0,
              "last-name":'please enter your last name',
              "quantity":'quantity must be a number',
              "email": 'please enter a valid email '};
delete jsonObj.result;
// or
delete jsonObj['result'];

演示

使用delete从JSON对象中删除该属性,然后将其传递给您的错误处理方法

var yourJSONObj=JSON.parse(yourJsonString);

delete yourJSONObj.result;

这样尝试

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

然后,您可以迭代或执行所需的操作。

试试这个:使用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>';

这是演示

如果您有字符串,请执行JSON.parse转换为json对象。 否则,请首先检查是否存在错误,如果是,则在删除第一个属性后对对象进行迭代。

演示: http//jsfiddle.net/abhitalks/NE5v5/

示例代码:

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.

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