JQuery 3 ways to iterate over a json array

  • 2020-03-30 04:17:28
  • OfStack

Use each to traverse


     $(function () {            var tbody = "";
           //-- traversing the use of object. Each --
           //Object syntax JSON data format (when a server-side callback comes back in a JSON data format, the JSON format must be guaranteed, and the callback Object must be converted using the eval function (otherwise it will not get Object). This article does not go into the details of the data issues of server-side callbacks, we will customize the object directly            var obj = [{ "name": " A navy ", "password": "123456"}];
           $("#result").html("------------ Traverse object .each The use of -------------");
           alert(obj); //Is an object
           //Now iterate through
using each            $.each(obj, function (n, value) {
               alert(n + ' ' + value);
               var trs = "";
               trs += "<tr><td>" + value.name + "</td> <td>" + value.password + "</td></tr>";
               tbody += trs;
           });            $("#project").append(tbody);        });

2. Jquery traverses and parses json object 1:


 var json = [{dd:'SB',AA:' East east ',re1:123},{cccc:'dd',lk:'1qw'}];
 for(var i=0,l=json.length;i<l;i++){
    for(var key in json[i]){
        alert(key+':'+json[i][key]);
    }
 }

Jquery traverses and parses json object 2

There is the following json object:


var obj ={ " name " : "Feng Juan" , " password " : " 123456 " , " department " : Technical department , " sex " : " Female" , " old " :30};

Traversal method:

for(var p in obj){
    str = str+obj[p]+',';
    return str;
}

PS: here again for you to recommend a few more practical json online tools for your reference:

Online JSON code verification, verification, beautification, formatting tools:
(link: http://tools.jb51.net/code/json)

JSON online formatting tool:
(link: http://tools.jb51.net/code/jsonformat)

Online XML/JSON interconversion tool:
(link: http://tools.jb51.net/code/xmljson)

Json code online formatting/beautification/compression/editing/conversion tools:
(link: http://tools.jb51.net/code/jsoncodeformat)

Online json compression/escape tool:

(link: http://tools.jb51.net/code/json_yasuo_trans)

C language style /HTML/CSS/json code format beautification tool:
(link: http://tools.jb51.net/code/ccode_html_css_json)


Related articles: