json:{ "justIn": [{ "textId": "123", "text": "Hello,geovindu", "textType": "Greeting" },{ "textId": "514", "text":"What's up?", "textType": "Question"...
json:
{ "justIn": [ { "textId": "123", "text": "Hello,geovindu", "textType": "Greeting" }, { "textId": "514", "text":"What's up?", "textType": "Question" }, { "textId": "122", "text":"Come over here", "textType": "Order" } ], "recent": [ { "textId": "1255", "text": "Hello,sibodu", "textType": "Greeting" }, { "textId": "6564", "text":"What's up?", "textType": "Question" }, { "textId": "0192", "text":"Come over here", "textType": "Order" } ], "old": [ { "textId": "5213", "text": "Hello,geovindu", "textType": "Greeting" }, { "textId": "9758", "text":"What's up?", "textType": "Question" }, { "textId": "7655", "text":"Come over here", "textType": "Order" } ] }
<script src="Scripts/jquery-1.9.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { $("#Link").click(function () { $.ajax({ type: "GET", url: "jsonfile/5.json", dataType: "json", success: function (data) { $.each(data, function (k, v) { $("#mapinfo").append(k + ', ' + "<br/><hr/>"); $.each(v, function (k1, v1) { //$("#title").append(k); $("#info").append(k1 + ' ' + v1.textId + ' ' + v1.text + ' ' + v1.textType + "</div><hr/>"); }); }); // } }); return false; }); }); </script> </head> <body> <input type="button" id="Link" value="query"/> <div id="title"></div> <div id="content"></div> <div id="mapinfo"></div> <div id="info"></div> </body> </html>
json:
{ "district": [ { "did": "1", "name": "武昌區", "communitys": { "community": [ { "cid": "21", "name": "安順家園", "url": "asjy", "address": "武昌區中北路23號", "x": "114.33830023", "y": "30.55309607", "img": "com21.png", "hot": "0", "groupbuy": "0", "estates": { "estate": [ { "name": "竹居", "value": "Z" }, { "name": "梅嶺", "value": "M" }, { "name": "蘭亭", "value": "L" }, { "name": "菊坊", "value": "J" } ] } }, { "cid": "25", "name": "百瑞景中央生活區", "url": "brj", "address": "武昌武珞路586號", "x": "114.33729172", "y": "30.52570714", "img": "com25.png", "hot": "0", "groupbuy": "0", "estates": { "estate": [ { "name": "南一區", "value": "S" }, { "name": "北一區", "value": "N" }, { "name": "東二區", "value": "E" }, { "name": "西二區", "value": "W" } ] } },
<title>json jquery 遍歷json對象 數組 geovindu</title> <script src="Scripts/jquery-1.9.1.min.js" type="text/javascript"></script> <script type="text/javascript"> ///對象語法JSON數據格式(當伺服器端回調回來的對象數據格式是json數據格式,必須保證JSON的格式要求,回調的對象必須使用eval函數進行轉化(否則將得不到Object)。本文不作詳細介紹伺服器端回調的數據問題,我們將直接自定義對象) $(document).ready(function () { $('#button').click(function () { $.ajax({ type: "GET", url: "jsonfile/4.json", dataType: "json", success: function (data) { //var obj = eval(data.district); $(data.district).each(function (index, value) { // var val = obj[index]; $("#title").append(index); $.each(value, function (k1, v1) { $("#content").append(k1 + ' ' + v1 + "</div><hr/>"); if (k1 == "communitys") { $.each(v1, function (q1, qname) { $("#mapinfo").append(q1 + ' ' + qname + "</div><hr/>"); if (q1 == "community") { $.each(qname, function (t1, tname) { $("#result").append(t1 + ', ' + tname.cid +tname.name+tname.address+tname.x+tname.y+tname.img+ "</div><hr/>"); }); } }); } }); }); } }); return false; }); }); </script> </head> <body> <div>點擊按鈕獲取JSON數據</div> <input type="button" id="button" value="確定" /> <div id="title"></div> <div id="content"></div> <div id="mapinfo"></div> <div id="result"></div> <div id="info"></div> </body> </html>
How to select json item from the array
<title>json jquery 遍歷解析json對象 How to select json item from the array</title> <script src="Scripts/jquery-1.9.1.min.js" type="text/javascript"></script> <script type="text/javascript"> //http://stackoverflow.com/questions/6107039/how-to-select-json-item-from-the-array //http://stackoverflow.com/questions/4992383/use-jquerys-find-on-json-object var JSON = { "infos": { "info": [ { "startYear": "1900", "endYear": "1930", "timeZoneDesc": "geovindu..", "timeZoneID": "1", "note": { "notes": [ { "id": "1", "title": "Mmm"}, { "id": "2", "title": "Wmm"}, { "id": "3", "title": "Smm"} ] }, "links": [ { "id": "1", "title": "Red House", "url": "http://infopedia.nl.sg/articles/SIP_611_2004-12-24.html"}, { "id": "2", "title": "Joo Chiat", "url": "http://www.the-inncrowd.com/joochiat.htm"}, { "id": "3", "title": "Bake", "url": "https://thelongnwindingroad.wordpress.com/tag/red-house-bakery"} ]} ] } }; var infoLength= JSON.infos.info.length; for (infoIndex = 0; infoIndex < infoLength; infoIndex++) { var notesLength= JSON.infos.info[infoIndex].note.notes.length; for (noteIndex = 0; noteIndex < notesLength; noteIndex++) { alert(JSON.infos.info[infoIndex].note.notes[noteIndex].title); } } </script>
查找參考:
//http://jsonselect.org/#tryit
//http://goessner.net/articles/JsonPath/
//https://github.com/lloyd/JSONSelect
http://www.codeproject.com/Tips/172224/Selecting-JSON-Objects
json:
[{ "TEST1": 45, "TEST2": 23, "TEST3": "DATA1" }, { "TEST1": 46, "TEST2": 24, "TEST3": "DATA1" }, { "TEST1": 47, "TEST2": 25, "TEST3": "DATA3"}];
function sql(s) { var returnObj = new Array(); var cntr = 0; var cnt; for (var bb = 0; bb < s.from.length; bb++) { //$.each(s.from, function(bb) { var ifConditions = new Array(); for (cnt = 0; cnt < s.where.length; cnt++) { ifConditions[cnt] = new Object(); var where = ""; if (s.where[cnt].OPERATOR.indexOf("=") == 0) where = "=="; if (s.where[cnt].VALUE.indexOf("'") > -1) ifConditions[cnt] = eval("'" + eval("s.from[bb]." + (eval("s.where[" + cnt + "].KEY"))) + "'" + where + eval("s.where[" + cnt + "].VALUE")); else ifConditions[cnt] = eval(eval("s.from[bb]." + (eval("s.where[" + cnt + "].KEY"))) + where + eval("s.where[" + cnt + "].VALUE")); } var comparedOutput = true; for (cnt = 0; cnt < s.conditions.length; cnt++) { var condition = ""; switch (s.conditions[cnt].CONDITION.toUpperCase()) { case "AND": condition = "&&"; break; case "OR": condition = "||"; break; } comparedOutput = comparedOutput && eval("ifConditions[" + s.conditions[cnt].Condition1 + "]" + condition + "ifConditions[" + s.conditions[cnt].Condition2 + "]"); } if (comparedOutput) { var result = {}; var cols = s.select.split(","); for (var cnt = 0; cnt < cols.length; cnt++) { result[cols[cnt]] = eval("s.from[bb]." + cols[cnt]); } returnObj.push(result); } } return returnObj; }
function sql(s) { var returnObj = new Array(); var cntr = 0; $.each(s.from, function(bb) { var ifConditions = new Array(); $.each(s.where, function(cnt) { ifConditions[cnt] = new Object(); var where = ""; if (s.where[cnt].OPERATOR.indexOf("=") == 0) where = "=="; if (s.where[cnt].VALUE.indexOf("'") > -1) ifConditions[cnt] = eval("'" + eval("s.from[bb]." + (eval("s.where[" + cnt + "].KEY"))) + "'" + where + eval("s.where[" + cnt + "].VALUE")); else ifConditions[cnt] = eval(eval("s.from[bb]." + (eval("s.where[" + cnt + "].KEY"))) + where + eval("s.where[" + cnt + "].VALUE")); }); var comparedOutput = true; $.each(s.conditions, function(cnt) { var condition = ""; switch (s.conditions[cnt].CONDITION.toUpperCase()) { case "AND": condition = "&&"; break; case "OR": condition = "||"; break; } comparedOutput = comparedOutput && eval("ifConditions[" + s.conditions[cnt].Condition1 + "]" + condition + "ifConditions[" + s.conditions[cnt].Condition2 + "]"); }); if (comparedOutput) { var result = {}; var cols = s.select.split(","); for (var cnt = 0; cnt < cols.length; cnt++) { result[cols[cnt]] = eval("s.from[bb]." + cols[cnt]); } returnObj.push(result); } }); return returnObj; }
TEST1 = 45 OR TEST3 = ‘DATA1’
var selectedObjs = sql({ select: "TEST1,TEST2", from: a, where: [{ "KEY": "TEST1", "OPERATOR": "=", "VALUE": "45" }, { "KEY": "TEST3", "OPERATOR": "=", "VALUE": "'DATA1'"}], conditions: [{ "Condition1": "0", "CONDITION": "Or", "Condition2": "1"}] });