簡體   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