Jquery traverses the json object set for details, jqueryjson
This article uses case analysis to introduce three situations of jquery traversing json objects for your reference. The specific content is as follows:
First case: Common examples of jquery traversing json object sets
Jsp
$. Ajax ({url: "$ {applicationScope. rootpath} common/getContractPage.html? UserConId =$ {userConId} ", type:" post ", dataType:" json ", data :{}, success: function (jsonText) {if (jsonText) {var status = jsonText. status; var msg = jsonText. msg; if (status = '000000') {// alert (msg) When an exception occurs;} else {$. each (jsonText, function (I, item) {var pngPath = item [0]; var pngName = item [1] ;}}});
JsonText format:
{"Status": "200", "msg": [{"id": "1", "name": "n1" },{ "id ": "2", "name": "n2"}]} {"status": "500", "msg": "exception information "}
In java:
List pngFileList = new ArrayList (); // if (null! = PngFileList & pngFileList. size ()> 0) {JSONArray pngFileArray = JSONArray. fromObject (pngFileList);} if (null! = PngFileArray) {this. setTextAjax (pngFileArray. toString (); // The format of the exception // this. setTextAjax ("{\" status \ ": \" 500 \ ", \" msg \ ": \" "+ errormsg + "\"}"); // No record/*** ajax returns html, including the json form ** @ param responseContent */public void setTextAjax (String responseContent) {try {HttpServletResponse response = getHttpResponse (); response. setContentType ("text/html"); response. setCharacterEncoding ("UTF-8"); response. setHeader ("Pragma", "No-cache"); response. setHeader ("Content-Type", "text/html"); response. setHeader ("Cache-Control", "no-cache"); response. setDateHeader ("Expires", 0); PrintWriter out = response. getWriter (); out. print (responseContent); out. flush (); out. close ();} catch (IOException e) {e. printStackTrace ();} // ajaxResponse = new StringBufferInputStream (responseContent );}
Case 2:JQuery traverses JSON objects
Without saying anything else, paste the Code directly:
<script src="js/jquery-1.6.4.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $("#Link").click(function() { var objson = "[{Title:'Sjr',Content:'Library',summary:'summary'},{Title:'Sjr',Content:'Library',summary:[{sum0:'sum0'},{sum0:'sum1'},{sum0:'sum2'}]},{Title:'Sjr',Content:'Library',summary:[{sum0:'sum0'},{sum0:'sum1'},{sum0:'sum2'}]}]"; var obj = eval(objson); $(obj).each(function(index) { var val = obj[index]; if (typeof (val.summary) == "object") { $(val.summary).each(function(ind) { alert(val.Title + " " + val.Content + " " + val.summary[ind].sum0); }); } else { alert(val.Title + " " + val.Content + " " + val.summary); } }); }); });</script>
Case 3:Reading objects in a json string in jquery
Suppose we get the following json string from the server, including arrays. How can we traverse and read data?
Copy codeThe Code is as follows: {"result": null, "rows": [{"caishen": "East", "fushen": "Northwest", "huajiazi": "jiazi ", "id": 1, "nayin": "sea water", "shengmen": "South", "simen": "North", "wugui": "West ", "xishen": "southeast", "yanggui": "Southwest China", "yingui": "Northeast China" },{ "caishen": "Northeast China", "fushen ": "North", "huajiazi": "", "id": 2, "nayin": "zhangmu", "shengmen": "Northwest", "simen ": "Southwest China", "wugui": "southeast", "xishen": "East", "yanggui": "West", "yingui": "South "}, {"caishen": "West", "fushen": "East", "huajiazi": "", "id": 3, "nayin": "pomegranate ", "shengmen": "North", "simen": "Northwest", "wugui": "South", "xishen": "southeast", "yanggui": "Northeast ", "yingui": "Southwest China"}], "total": 3}
It is too difficult to use. each. Read it directly using js.
// Obtain the json object $. post ("json/godjson! Godlist ", function (data) {// data. rows returns a json string. You need to convert the json object var json = eval (data. rows) // The json variable is now an array object and read each array object directly. Outputs for (var I = 0; I <json. length; I ++) {alert (json [I]. caishen + "---------" + json [I]. xishen);} // remember that the returned data type must be json}, "json ");
The json object of the json string can be obtained and output in the preceding way.
I would like to share with you so many cases that I want to help you understand how jquery traverses json object sets. I hope this article will help you.