Four ways to collect json parsing to share

  • 2020-03-30 01:24:10
  • OfStack

Json is very useful in Web development, and as the carrier of data transmission, it is very common to parse the data returned by Json. Here are four ways to parse Json:

Part 1


var list1 = [1,3,4];
alert(list1[1]);
var list2 = [{"name":"leamiko","xing":"lin"}];
alert(list2[0]["xing"])
alert(list2[0].xing)

Part 2


var value = {
    "china":{
        "hangzhou":{"item":"1"},
        "shanghai":{"item":"2"},
        "chengdu":{"item":"3"}
    },
    "America":{
        "aa":{"item":"1"},
        "bb":{"item":"2"}   
    },
    "Spain":{
        "dd":{"item":"1"},
        "ee":{"item":"2"},
        "ff":{"item":"3"}   
    }
};
for(var countryObj in value)
{
    document.write(countryObj + ":<br />")
    //For (var cityObj in value.countryObj)
    for(var cityObj in value[countryObj])
    {
        document.write('    ' + cityObj + "<br />");
        for(var itemObj in value[countryObj][cityObj])
        {
            document.write("      "+ itemObj + value[countryObj][cityObj][itemObj] +"<br />")   
        }
    }   
}   

Explanation:

CountryObj is a property of the value object and clearly, value[countryObj] is the property value of the value object and here is a json object such as b, value[countryObj][cityObj] is the property value of josn object b which is also a json object, so value[countryObj][cityObj]["item"] can take the json object and temporarily become the value of c, Or the value [countryObj] [cityObj]. Item.

So it's important to know whether it's json or array.

Part 3


var value2 = {
    "china":[
        {"name":"hangzhou", "item":"1"},
        {"name":"shanghai", "item":"2"},
        {"name":"sichuan", "item":"3"}
    ],
    "America":[
        {"name":"aa", "item":"12"},
        {"name":"bb", "item":"2"}
    ],
    "Spain":[
        {"name":"cc", "item":"1"},
        {"name":"dd", "item":"23"},
        {"name":"ee", "item":"3"}
    ]
};

for (var countryObj in value2)
{
    document.write(countryObj + ":<br />")   
    for (var cityObj in value2[countryObj])
    {
        //You can use the document. Write ("   "+ value2[countryObj][cityObj]. Item + "<Br />" );
        document.write(cityObj + "  " + value2[countryObj][cityObj]["name"] + "<br />" );   
    }
}

Explanation:

CountryObj is the property name of the value2 object,value2[countryObj] is the property value of the value2 object in this case it's an array, cityObj is an element of the array, it's another json object, so value2[countryObj][cityObj]["name"] accesses the property value of the name of the object, The property value can also be accessed by value2[countryObj][cityObj].name.

Part 4


var value2 = {
    "china":[
        {"name":"hangzhou", "item":"1"},
        {"name":"shanghai", "item":"2"},
        {"name":"sichuan", "item":"3"}
    ],
    "America":[
        {"name":"aa", "item":"12"},
        {"name":"bb", "item":"2"}
    ],
    "Spain":[
        {"name":"cc", "item":"1"},
        {"name":"dd", "item":"23"},
        {"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[countryObj] property value in this example is an array, the length of the value2[countryObj].length array, the item of the value2[countryObj][I] array == json object.

Value2 [countryObj][I]["name"] gets the value of name, or value2[countryObj][I].name gets the value of name.


Related articles: