Iterating through the data of a JSON object using the for in loop

Source: Internet
Author: User


Using for in to iterate through the JSON object data, if the name in the data is a number, only valid for positive integer, then output the data as a positive integer, and the others will be output in the same order as defined in the original data.

    • Test the data for JSON objects named numbers as follows:
var = {    "Json_obj_data": {' title ': ' All ', ' name ': ' All promotions ', ' ID ': ' All '},     "" ": {" title ":" Best_room_rate "," Name ":" Best hostel Rate "," id ":" best_room_rate "},    " ten ": {" title ":" Summer Rate "," name ":" Summer Rate "," id ":" + "}    
40,20,10 can be enclosed in double quotes, the output is the same as for (var in json_obj_data) { alert (data ); // 10->20->40 }</script>
    • Test the JSON object data with the name string, with the following code:
var json_data = {"3promotion": {"title": "All", "room_promotion_id": "All"},                             "2promotion": {" Title ":" Best_room_rate "," roompromotion_id ":" Best_room_rate "},                            " 1promotion ": {" title ":" Summer " Rate "," room_promotion_id ":" + "}                            ;              for (var in json_data) {          alert (Promotion);//output is in the order defined above}
    • For JSON object data tests with names of numbers and strings, the code is as follows:
var json_data = {"3promotion": {"title": "All", "room_promotion_id": "All"},                             "2promotion": {" Title ":" Best_room_rate "," roompromotion_id ":" Best_room_rate "},                            " 1 ": {" title ":" Summer "," the " _promotion_id ":" "}                            };               for (var in json_data) {        alert (Promotion);//The data first outputs the name as a number and then outputs the name as a string in sequence}

There are two JSON object data tests with a number of names, and others as strings, with the following code:

var json_data = {"3promotion": {"title": "All", "room_promotion_id": "All"},                             "2promotion": {" Title ":" Best_room_rate "," roompromotion_id ":" Best_room_rate "},                            " 1 ": {" title ":" Summer "," the " _promotion_id ":" "},                            " 0 ": {" title ":" Expecial rate "," room_promotion_id ":" $ "}                            };              for (var in json_data) {      alert (Promotion);//Output is 0->1->3promotion-> 2promotion}

The JSON object data that has the name of a number, where there are negative numbers, is treated as a string, and the other is a string, the code is as follows:

 var  json_data = {"3promotion": {"title": "All", "room_promotion_id": "All" Span style= "color: #000000;" _},  "2promotion": {"title": "Best_room_rate", "roompromotion_id": "Best_room_rate" },  "1": {"title": "Summer", "room_promotion_id": "},  "0": {"title": "Expecial rate", "room_promotion_id": "},  "1": {"title": ' Demo3 ', ' room_promotion_id ': "Demo3" }             };  for  (var  promotion in   Json_data) {alert (promotion);//0->1->3promotion->2promotion- >-1}  

  includes numeric names that are less than 1, and names with negative numbers, and the code is tested as follows:

var json_data = {"3promotion": {"title": "All", "room_promotion_id": "All"},                             "2promotion": {" Title ":" Best_room_rate "," roompromotion_id ":" Best_room_rate "},                            " 1 ": {" title ":" Summer "," the " _promotion_id ":" "},                            " 0.25 ": {" title ":" Expecial rate "," room_promotion_id ":" $ "},                            " 1 " : {"title": ' Demo3 ', ' room_promotion_id ': "Demo3"}                            };              for (var in json_data) {    alert (promotion);//1->3promotion->2promotion->0.25- >-1
}

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.