Jquery parses json array strings

Source: Internet
Author: User

Recently, Jquery was used in our work to parse json strings. There are many examples of jquery parsing json single objects on the Internet, but there are not many examples of jquery parsing json arrays, here is a simple example to share with you. I have a limited level and hope you will criticize and advise me.

Json string of A json object array:

var str=[{"Price":12,"Name":"aaa","Age",22},{"Price":24,"Name":"bbb","Age",33}];

On the front-end, use the parseJSON method of jquery for parsing and use the jquery foreach Method for parsing. The Code is as follows:

var jsonarray= $.parseJSON(str);alert(jsonarray);$.each(jsonarray, function (i, n){    alert(n.Price);}

For jquery of different versions, the string format of the json object array to be parsed is also different (I used two different versions of jquery and I encountered this problem ), if the above Code cannot be parsed, use the eval function to wrap it. The Code is as follows:

var jsonarray= $.parseJSON(str);$.each(eval("(" + jsonarray+ ")"), function (i, n) {    alert(n.Price);}

When parsing the json object array string, you can also use alert to pop up $. after parseJSON (str) is parsed, if an object is displayed in the pop-up dialog box, the parsing is successful. You can use the object. the property name obtains the value of the corresponding property. If the pop-up is not an object, use eval () to wrap it to form an object, and then obtain the value of the corresponding property.

The above is my little experience in using jquery to parse arrays of json objects. I am looking forward to your comments.

 

 

  

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.