From---small bottle editor
The $.each () method accepts two parameters, the first is the collection of objects that need to be traversed (a collection of JSON objects), the second is the method to traverse, the method accepts two parameters, the first is the index of the traversal, and the second is the value that is currently traversed. Haha, with the $.each () method JSON parsing will be solved.
function Loadinfo () {
$.getjson ("Loadinfo", function (data) {
$ ("#info"). HTML ("");//Empty info content
$.each (data.comments, function (I, item) {
$ ("#info"). Append (
"<div>" + item.id + "</div>" +
"<div>" + item.nickname + "</div>" +
"<div>" + item.content + "</div>});
});
}
As above, Loadinfo is the requested address, function (data) {...} is the callback function after the request succeeds, data encapsulates the returned JSON object, in the following $.each (Data.comments,function (I,item) {...}) The Data.comments method directly reaches the JSON array contained within the JSON data:
[{"Content": "Very good", "id": 1, "nickname": "Nani"},{"content": "Yo Xi yo XI", "id": 2, "nickname": "Xiao Qiang"}]
The function in the $.each () method is to iterate over the array and insert it into the appropriate place by manipulating the DOM. During the traversal, we can easily access the currently traversed index ("I" in the code) and the currently traversed value ("item" in the code).
The results are as follows:
Original: http://www.cnblogs.com/codeplus/archive/2011/07/182109544.html
jquery gets JSON data