Four ways to share JSON parsing

Source: Internet
Author: User
Tags json

  This article mainly introduces four methods of JSON parsing, and a friend in need can refer to the

JSON is very useful for web development, and as a carrier of data delivery, how to parse the data returned by JSON is very common. Here are four ways to parse JSON:   Part 1   code is as follows: var list1 = [1,3,4]; Alert (list1[1]); var list2 = [{"Name": "Leamiko", "Xing": "Lin"}]; Alert (list2[0]["Xing"]) alert (list2[0].xing)     Part 2     code is as follows: var value = {    "a": {         "Hangzhou": {"Item": "1"},       "Shanghai": {"Item": "2"},     & nbsp   "Chengdu": {"Item": "3"}    },     "America": {      "AA": {"Item": "1"}, &N Bsp       "BB": {"Item": "2"}       },     "Spain": { ,       DD ": {" Item ":" 1 "},        " ee ": {" Item ":" 2 "},      " FF ": {" Item ":" 3 "}  &nbs P    }}; for (var countryobj in value) {    document.write (countryobj + ": <br/>")    //useless for (Var cityo BJ in Value.countryobj)     for (Var cityobj inValue[countryobj])     {        document.write ('     ' + cityobj + "<br/>") ;         for (Var itemobj in Value[countryobj][cityobj])         {    &NB Sp       document.write ("     " + Itemobj + value[countryobj][cityobj][itemobj] + "<br/> ")            {   }   }        explanation:   Country OBJ is a property of the value object, Value[countryobj] is the property value of the value object for a JSON object such as B,value[countryobj][cityobj] to Josn object B It is also a JSON object, so value[countryobj][cityobj]["item" can take the value of a JSON object to temporarily become C, or Value[countryobj][cityobj].item.   It's critical to distinguish between JSON and array.   Part 3     code is as follows: var value2 = {      "" ": [        {" name ":" Hangzh ou "," Item ":" 1 "},         {" name ":" Shanghai "," Item ":" 2 "},    ,     {" name ":" Sichua N "," Item ":" 3 "}   &nbsp ],     "America": [        {"name": "AA", "Item": "A"},      ,   {"Name": " BB "," Item ":" 2 "}    ],    " Spain ": [   ,     {" Name ":" CC "," Item ":" 1 "},   &N Bsp     {"name": "DD", "Item": "A"},         {"name": "EE", "Item": "3"}    ]};   for (Var countryobj in value2) {    document.write (countryobj + ": <br/>")      &NB Sp for (Var cityobj in Value2[countryobj])     {       //Can be document.write (" " + value2 [Countryobj] [Cityobj].item + "<br/>");         document.write (cityobj + " " + value2[countryobj][cityobj]["name"] + "<br/>");       }}       explanation:   Countryobj for value2 object property name, Value2[countryobj] for value2 object attribute value in this In an example, it is an array, cityobj is an element of an array, and it is another JSON object, so value2[countryobj][cityobj]["name" accesses the genus of the object's nameValue, you can also access the property value through Value2[countryobj][cityobj].name.   Part 4     code as follows: var value2 = {    "" ": [        {" name ":" Hangzhou "," I TEM ":" 1 "},         {" name ":" Shanghai "," Item ":" 2 "},         {" name ":" Sichuan "," it Em ":" 3 "}    ],    " America ": [        {" name ":" AA "," Item ":" A "},         {"name": "BB", "Item": "2"}    ],     "Spain": [        {"Name": "CC", "Item": "1"},         {"name": "DD", "Item": ""},    ,     {"name": "EE", "Item": "3" }    ]};       for (Var countryobj in value2)     {        document.write (countryobj + ": <br/>")           //document.write (" " + value2[countryobj].length);         for (var i = 0;i < value2[countryobj].length; i++)         {            document.write (" " + value2[countryobj][i ] ["name"] + "<br/>");            {   }     Explanation:   countryobj value2 object property name, Value2[country OBJ] Property value in this case is an array, value2[countryobj].length the length of the array, the item = = JSON object of the Value2[countryobj][i] array.   value2[countryobj][i]["name" gets the value of name, or you can use Value2[countryobj][i].name to get the value of name.  

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.