简体   繁体   English

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

[英]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 this: 尝试这个:

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'];

DEMO 演示

Use delete to remove the property from your JSON object, then pass it to your error handling method 使用delete从JSON对象中删除该属性,然后将其传递给您的错误处理方法

var yourJSONObj=JSON.parse(yourJsonString);

delete yourJSONObj.result;

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>';

Here is the DEMO 这是演示

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.

相关问题 如何从JSON对象获取元素值,然后先将其转换为HTML,然后转换为String - How to get element value from JSON object and first convert it to HTML and then to String 如何访问 JSON 对象数组的第一个元素? - How to access first element of JSON object array? 如何从json文件中读取数据并在angularJS中作为字符串或对象返回? - How to read the data from json file and return as string or object in angularJS? 如何将JSON对象的第一个元素移到末尾? - How to move first element of the JSON object to the end? 如何在不知道密钥的情况下获取 JSON object 的第一个密钥对元素并在节点红色中将其删除 - How to get first key pair element of JSON object without knowing key and deleting it in node red Javascript JSON 每个只返回第一个 object 字符串 - Javascript JSON for each return only first object string AngularJS如何返回字符串而不是Json对象 - AngularJS how return a string instead of Json object 如何将此 json object 返回到字符串中 - How can I return this json object into a string jQuery $ .each()仅返回Json对象的第一个元素 - jQuery $.each() returns only the first element from a Json object 如何使用jquery或javascript从对象内部将当前对象作为JSON字符串返回? - How can I return the current object as a JSON string from inside the object with jquery or javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM