Javascript code for reading JSON data cyclically

Source: Internet
Author: User

The JSON format string returned from the server end to the client:
Var str = '[{"uname": "Wang Qiang", "day": "2010/06/17" },{ "uname": "Wang haiyun", "day ": "2010/06/11"}] ';

We convert it to a JSON object: var jsonList = eval ("(" + str + ")"). At this time, if you use breakpoint tracking to view this jsonList object, you will actually find that, its length is 2, that is, jsonList. length = 2. Each of its items is a separate JSON object. See:


In this case, if we do not know the "uname" and "day" key values, we start to execute the loop operation on jsonList.
Copy codeThe Code is as follows:
For (var I = 0; I <jsonList. length; I ++ ){

For (var key in jsonList [I]) {
Alert ("key:" + key + ", value:" + jsonList [I] [key]);
}
}

In this way, we can easily obtain the key and value values we need.

Related Article

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.