Parse JSON data using jQuery

Source: Internet
Author: User

In the previous analysis of the Ajax development instance in Struts2, we obtained the JSON data of the comments object. In this article, we will use jQuery for data parsing.

Let's take parsing the JSON data of the comments object in the previous example as an example, and then summarize the method for parsing JSON data in jQuery.

The JSON data obtained in the preceding example is a nested JSON:

 
 
  1. {"Comments": [{"content": "pretty good", "id": 1, "nickname": "Nana" },{ "content ": "", "id": 2, "nickname": "Xiaoqiang"}]}

To obtain JSON data, there is a simple method $. getJSON () in jQuery.

The following describes $. getJSON () of the official API:

JQuery. getJSON (url, [data,] [success (data, textStatus, jqXHR)])

UrlA string containing the URL to which the request is sent.

DataA map or string that is sent to the server with the request.

Success (data, textStatus, jqXHR) A callback function that is executed if the request succeeds.

The callback function accepts three parameters, the data returned by the first book, the second is the state, and the third is the XMLHttpRequest of jQuery. We only use the first parameter.

$. Each () is used to parse JSON data in the callback function. The following is an official document:

JQuery. each (collection, callback (indexInArray, valueOfElement ))

CollectionThe object or array to iterate over.

Callback (indexInArray, valueOfElement) The function that will be executed on every object.

$. The each () method accepts two parameters. The first is the object set to be traversed (the JSON object set), and the second is the Traversal method. This method also accepts two parameters, the first is the traversal index, and the second is the current traversal value. Haha, with the $. each () method JSON resolution, you can solve it easily.

 
 
  1. Function loadInfo (){
  2. $. GetJSON ("loadInfo", function (data ){
  3. $ ("# Info" ).html (""); // clear info content
  4. $. Each (data. comments, function (I, item ){
  5. $ ("# Info"). append (
  6. "<Div>" + item. id + "</div>" +
  7. "<Div>" + item. nickname + "</div>" +
  8. "<Div>" + item. content + "</div>
  9. });
  10. });
  11. }

As above, loadinfo is the request address, function (data ){...} is the callback function after the request is successful. data encapsulates the returned JSON object, in the following $. each (data. comments, function (I, item ){...}) data. comments directly reach the JSON array contained in the json data:

 
 
  1. [{"Content": "Great", "id": 1, "nickname": "Nana" },{ "content": "", "id": 2, "nickname": "Xiaoqiang"}]

The function in the $. each () method traverses the array and inserts the DOM into a proper place. During the traversal process, we can easily access the current traversal index ("I" in the Code) and the current traversal value ("item" in the Code).

The running result of the preceding example is as follows:

If the returned JSON data is complex, you only need to traverse more $. each. For example, JSON data is as follows:

 
 
  1. {"Comments": [{"content": "pretty good", "id": 1, "nickname": "Nana" },{ "content ": "", "id": 2, "nickname": "Xiaoqiang"}],
  2. "Content": "You are a wood man, haha. "," Infomap ": {" gender ":" male "," Occupation ":" programmer "," blog ":" http: \ // www.cnblogs.com \/codeplus \/"}," title ":" 123 Wood Man "}

Js:

 
 
  1. Function loadInfo (){
  2. $. GetJSON ("loadInfo", function (data ){
  3. $ ("# Title"). append (data. title + "
  4. $ ("# Content"). append (data. content + "
  5. // Jquery parses map data
  6. $. Each (data. infomap, function (key, value ){
  7. $ ("# Mapinfo"). append (key + "----" + value + "<br/>
  8. });
  9. // Parse the Array
  10. $. Each (data. comments, function (I, item ){
  11. $ ("# Info"). append (
  12. "<Div>" + item. id + "</div>" +
  13. "<Div>" + item. nickname + "</div>" +
  14. "<Div>" + item. content + "</div>
  15. });
  16. });
  17. }

It is worth noting that when $. each () traverses the Map, the parameters in function () are key and value, which is very convenient.

Running Effect of the above example:

JQuery is very powerful. For more information, see this document.

Original article: http://www.cnblogs.com/codeplus/archive/2011/07/18/2109544.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.